Error: How to store values from a for loop?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
parslee
am 3 Okt. 2021
Kommentiert: parslee
am 3 Okt. 2021
I wrote a for loop so that I can extract just the index value since the getDataTips extracts [X,Y], index, and [R,G,B] where the index values are 466.833, 405.573, and so on. (See attached image).
There are 11 data tips I would like to extract.
And I'm trying to store all the values from the for loop into an array using the following:
s = 11;
a1 = zeros(11,1);
for i = 1:s
x(i+1) = content{1,1}{i,1}{2};
a1(11,i) = x(i+1);
end
This outputs an error message which says: "Unable to perform assignment because the left and right sides have a different number of elements" for the line: x(i+1) = content{1,1}{i,1}{2}.
When I do,
for i = 1:s
content{1,1}{i,1}{2}
end
It outputs:
ans = 'Index 466.833'
ans = 'Index 405.573'
I know that I didn't write my for loop coding correctly when attempting to put it into an array, but I'm not sure what's wrong because this is my first time writing a for loop.
So any help on this would be much appreciated!
2 Kommentare
Image Analyst
am 3 Okt. 2021
It really depends on what's in content. Wee need to know that to know how to reference the element inside it. But to clarify what you're doing, I'd break it apart into separate terms like this:
% Extract the contents of the (1,1) cell.
% It is also a cell array, but with s rows and unknown number of columns.
cell1 = content{1,1};
% Preallocate cell arrays x and a.
x = cell(s + 1, 1);
a = cell(11, s + 1);
% Loop over the first s rows of cell1.
for k = 1 : s
% Get contents of the k'th cell in column 1.
% For some reason, it also appears to be a cell array of at least 2 elements.
thisCell = cell1{k, 1}
% Get the second cell of thisCell and assign it to x.
% x will also be a cell array.
x(k + 1) = thiscell(2);
a1(11, k) = x(k + 1);
end
It's a really complicated structure having cell arrays inside cell arrays inside cell arrays. You should really simplify it somehow.
PLEASE ATTACH content IN A .MAT FILE if you need anymore help.
save('Gabrielle.mat', 'content');
Also see the FAQ on cell arrays:
Akzeptierte Antwort
Walter Roberson
am 3 Okt. 2021
s = 11;
a1 = zeros(s,1);
for i = 1:s
x = sscanf(content{1,1}{i,1}{2}, 'Index %f');
if isempty(x)
x = nan;
else
x = x(1);
end
a1(i) = x;
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!