Not having my first "FOR LOOP" numbers assigned inside my MATLAB code

1 view (last 30 days)
Hi all,
I was wondering if anyone could assist me with a silly question on MATLAB! The story is that I'm generating some numbers in my code, catgorized into two separate ranges. Now, I need to implement those numbers in the other part of my code, as follows:
xs = strings(1,24);
for i= 1:24
if x(i)==1
xs(i) = "Set WinS_Construct = R1WinConstruction";
elseif x(i)==2
xs(i) = "Set WinS_Construct = R2WinConstruction";
elseif x(i)==3
xs(i) = "Set WinS_Construct = R3WinConstruction";
elseif x(i)==4
xs(i) = "Set WinS_Construct = R4WinConstruction";
end
end
xs = strings(25,29);
for i= 25:29
if x(i)==13
xs(i) = "13";
elseif x(i)==14
xs(i) = "14";
elseif x(i)==15
xs(i) = "15";
elseif x(i)==16
xs(i) = "16";
elseif x(i)==17
xs(i) = "17";
elseif x(i)==18
xs(i) = "18";
end
end
While I could sucessfully generate the "x" numbers, I'm not sure why MATLAB does not generate any number for my first loop. To calrify more, my "xs" strings includes only numbers for i=25:29- and I'm missing "xs" strings for i=1:24!
Any help would be greatly appreciated!
Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 25 Apr 2021
xs = strings(25,29);
That line of code requests that all of xs be overwritten with a 25 by 29 array of string objects.
Remove that line and change
xs = strings(1,24);
to
xs = strings(1,29);

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!