1

How to output class property so that it can be accessed in MATLAB's terminal? In my case, ClassA stores p array and shows output like:

 ClassA with properties:

    p: [3x3 double]

But when I want to access the array, its always says undefined function or variable. Although its public.

My Code:

classdef Input
    properties
        p
    end
    methods
        function obj = Input()
            [obj.p] = input('Enter array like [a b c; d e f;]');
        end
    end
end
1
  • 1
    Please use proper Indented code (Press ctrl+I in your matlab editor), it's easier to read. Commented Apr 18, 2014 at 21:10

2 Answers 2

1

You probably need to clear all instances of Input classes and rehash your path to update the definition of the class.

I get:

>> myIn = Input;
Enter array like [a b c; d e f;][1 2 3; 4 5 6]
>> myIn
myIn = 
  Input with properties:

    p: [2x3 double]
>> myIn.p
ans =
     1     2     3
     4     5     6
Sign up to request clarification or add additional context in comments.

5 Comments

U r very true, please tell me is there any other way like some Property Access Method to make this default, like I jist Enter p in my case and it shows value. ?
I need so because i'm also working with dynamic variables using eval and in your case, it couldn't access that. I need a method which is public and automatically stores in terminal variables.
I'm not sure I understand fully, but you can just set a default in properties (p = [1 2 3; 4 5 6];) and put logic in the constructor to reject the input if nothing (just Enter) is received (xx = input('Enter array like [a b c; d e f;]'); if ~isempty(xx), obj.p = xx; end)
@AhsanAli I'm not sure if you got what you needed out of my last comment, but I'm glad you accepted the answer. If it's helpful, upvotes are also appreciated. Thanks! :)
Yeah it is useful, and i'm totally satisfied with that. :)
0

When you are using input, you have to enter valid matlab code. Your command asks for a input like [a b c; d e f;], but the variables a-f are unknown. If you are intending to create a char array, use ['abc';'def']

1 Comment

No, I'm not intending to create char array, I want double data type values, What should I do in that case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.