why does this not work ?

for n=1:4 % size(PIDS,1);
BC = {n,4};
BC{n,1} = {PIDS{n,1}; FNS{n,1}; PPNameS{n,1}; APS{n,1}};
BC{n,2} = {PIDS{n,2}; FNS{n,2}; PPNameS{n,2}; APS{n,2}};
BC{n,3} = {PIDS{n,3}; FNS{n,3}; PPNameS{n,3}; APS{n,3}};
BC{n,4} = {PIDS{n,4}; FNS{n,4}; PPNameS{n,4}; APS{n,4}};
end
BC

5 Kommentare

Jan
Jan am 3 Apr. 2017
We cannot know the reason, when you do not tell us what's going wrong. Do you get an error message or do the results differ from your expectations?
Anne
Anne am 3 Apr. 2017
'Index exceeds matrix dimensions'
then I tried to convert BC into an empty cell.
then ive got another error
Undefined variable "cell" or class "cell".
Jan
Jan am 3 Apr. 2017
Bearbeitet: Jan am 3 Apr. 2017
Please, Anne, post the complete error message. We do not have crystal balls and cannot guess the reason of the problems. Show us your code and care for a copy of the complete message. Obviously there is an error in your trial "to convert BC into an empty cell" - but I neitehr understand what "converting to an empty cell" means, nor how you've tried to implement this.
Anne
Anne am 3 Apr. 2017
'Index exceeds matrix dimensions' is the complete error message.
Jan
Jan am 3 Apr. 2017
Bearbeitet: Jan am 3 Apr. 2017
No, a complete error message should contain the failing line also. Are you running tis in the command window? If so, create a script or even better a function. Then debugging is easier und you (and the readers) do not have to guess, where the error occurres.
I've edited my answer.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 3 Apr. 2017
Bearbeitet: Jan am 3 Apr. 2017

0 Stimmen

You overwrite BC in each iteration. Perhaps you want to move the pre-allocation before the loop:
BC = cell(4, 4); % Not: {4, 4};
for n = 1:4 % size(PIDS,1);
BC{n,1} = {PIDS{n,1}; FNS{n,1}; PPNameS{n,1}; APS{n,1}};
BC{n,2} = {PIDS{n,2}; FNS{n,2}; PPNameS{n,2}; APS{n,2}};
BC{n,3} = {PIDS{n,3}; FNS{n,3}; PPNameS{n,3}; APS{n,3}};
BC{n,4} = {PIDS{n,4}; FNS{n,4}; PPNameS{n,4}; APS{n,4}};
end
"BC = {n,4}" means, that BC is set to a [1 x 2] cell array. Is this intented? Afterwards the elements {n, 1:4} are added. But the values from the former itereations are overwritten.
To get the reason of the error message, write the code in a script or better a function. Then typ this in the command window:
dbstop if error
Now run your code again until it stops at the error. Inspect the variables and the indices: At least one of the arrays has less elements than you request.

1 Kommentar

Image Analyst
Image Analyst am 3 Apr. 2017
Anne, read this and this before you respond again. Also post some sample data for PIDS, FNS, PPNameS, and APS so we can try things to help you.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 3 Apr. 2017

Kommentiert:

am 3 Apr. 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by