my for loop doesn't seem to be working. I want to add the correct letter to a string based on the equality of two arrays. How can I make this wok?

1 Ansicht (letzte 30 Tage)
dec = table2array(decodeMessage);
ix = -40:3:38;%collating the degrees corresponding to the letters
message = "";
al = "abcdefghijklmnopqrstuvwxyz ";%corresponding letter array
volm = zeros(1,16);
in = 1;
for j=1:1:16
volm(j) = mean(dec([in:in+14],2));
in = in+15
end
% have collected average values in volm
degm = (volm-co(2))/co(1);% making x the subject of y=mx+c
round(degm);
%rounded to the nearest integer, now we have our final values
%% this is where it stops working, how do i fix this? the message string is blank
for k = 1:16
for o = 1:27
if ix(o) == degm(k)
message = message+ al(o);
else
continue
end
end
end
  4 Kommentare
Natalia Mehta
Natalia Mehta am 24 Apr. 2021
Thank you so much! Turns out that it was because i didn't save degm to a variable, my bad ahah
Clayton Gotberg
Clayton Gotberg am 24 Apr. 2021
Bearbeitet: Clayton Gotberg am 24 Apr. 2021
I can safely say we've all been there! After all, I missed it in my first reading too. I'm glad I could help.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Clayton Gotberg
Clayton Gotberg am 24 Apr. 2021
It seems the problem is that al is a string and therefore counts as one object for MATLAB. You can remedy this by switching to a char array (using single quotes instead of double quotes) or by splitting the string into an array of strings.
single_string = "abcdefghijklmnopqrstuvwxyz "; % 1x1 object so can't be indexed
% Solutions:
string_array = ["a" "b" "c" "d" "e" ... ]; % And so on
% or
char_array = char("abcdefghijklmnopqrstuvwxyz ");
% or
char_array = 'abcdefghijklmnopqrstuvwxyz ';
I bet your error was something like "Index exceeds array bounds". Please make sure you include any errors when you make your post so we can help you as much as possible!
  4 Kommentare
Natalia Mehta
Natalia Mehta am 24 Apr. 2021
My apologies for not including the value of co(), it's something i used earlier in the program.
Clayton Gotberg
Clayton Gotberg am 24 Apr. 2021
That makes sense. If after all of this you're still not getting an output, can you attach the code or a workspace after you run the code to a comment? I can dig a little deeper into the variables and see if there's something else causing the problem. My gut instinct is that there's a problem with the if ix(o) == degm(k) part of the code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by