0

I’ve got a series of matrices

zeroingMatrix{i} % i going from 1 to 'matrixQuantity'

I need to pass these to another Callback function. So the idea was:

for i = 1:matrixQuantity
    setappdata(0, 'zeroingMatrix{'i '}', zeroingMatrix{i});
end

and get it back with:

for i = 1:matrixQuantity
    zeroingMatrix{i} = getappdata(0, 'zeroingMatrix{' i '}');
end

but apperantly this 'zeroingMatrix{'i '}' is not the correct syntax and i can't figure out the right one. I tried all kinds of bracket combinationss but it won't let me do it.

When I try to start it, Matlab gives me the standard error:

Error: File: RackReader.m Line: 184 Column: 36 Unexpected MATLAB expression.

line 184 being the setappdata line.

This is not the only data I'm passing inbetween functions, but the first with a variable in it. Everything works fine exept this one.

Anyone else ever ran into this problem or has a better idea?

Thanks so much in advance

Mike

Mike-C
  • 43
  • 7
  • try using `num2str(i)` in the second parameter. So it looks `'zeroingMatrix{' num2str(i) '}'` –  Jul 12 '16 at 15:02
  • thx... You still need [ ] and you can't use { } i guess... but the rest works... thanks – Mike-C Jul 12 '16 at 15:10

2 Answers2

1

Your parameter name for setappdata has to just be a valid string and you're not able to construct that string using the syntax 'string'1'other'. You would need to use sprintf or num2str to construct the string.

field = sprintf('zeroingMatrix{%d}', i);

% OR
field = ['zeroingMatrix{', num2str(i), '}'];

Also, a better approach would be to just store the entire cell array in there and index it after you retrieve it.

% Set the value
zeroingMatrix{i} = newval;
setappdata(0, 'zeroingMatrix', zeroingMatrix)

% Then later get the value
zeroingMatrix = getappdata(0, 'zeroingMatrix');
zeroingMatrix{i}
Suever
  • 64,497
  • 14
  • 82
  • 101
0

So BlackAdder pushed me into the right direction:

for i = 1:matrixQuantity
    setappdata(0, ['zeroingMatrix' num2str(i)], zeroingMatrix{i});
end

and to get it back:

for i = 1:matrixQuantity
    zeroingMatrix{i} = getappdata(0, ['zeroingMatrix' num2str(i)]);
end
Mike-C
  • 43
  • 7
  • I would recommend storing the data in a single place rather than constantly altering the field that you save it under. See my approach below. – Suever Jul 12 '16 at 18:17