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
chappjc
  • 30,359
  • 6
  • 75
  • 132
  • 1
    Please use proper Indented code (Press ctrl+I in your matlab editor), it's easier to read. – Daniel Apr 18 '14 at 21:10

2 Answers2

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
chappjc
  • 30,359
  • 6
  • 75
  • 132
  • 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. ? –  Apr 18 '14 at 21:59
  • 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. –  Apr 18 '14 at 22:01
  • 1
    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`) – chappjc Apr 18 '14 at 22:03
  • @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! :) – chappjc Apr 24 '14 at 23:39
  • Yeah it is useful, and i'm totally satisfied with that. :) –  Apr 24 '14 at 23:42
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']

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • No, I'm not intending to create char array, I want double data type values, What should I do in that case? –  Apr 18 '14 at 21:22