Array in loop - Index exceeds the number of array elements (3) - problem

R = string(data{:});
conf_val = [R(1) "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27"];
for ii=1:27
if isempty(R(ii))
x = "nill";
else
x = R(ii);
end
text_str{ii} = ['Confidence: ' convertStringsToChars(x),'%s' '%'];
end
data = textscan(fileID,'%s');
fclose(fileID);
R = vertcat(data{:});
for ii=1:27
text_str{ii} = R{ii};
end
text string should have 27 elements.
I need a character vector of 27 strings read from a file.
if there are not 27 elements in my file, I would like to display 'nil' for that element at the missing index
Please can someone help

6 件のコメント

Saud Alfalasi
Saud Alfalasi 2021 年 2 月 20 日
@Image Analyst Please can you help
Saud Alfalasi
Saud Alfalasi 2021 年 2 月 20 日
@Jan Hi please can you help
Mario Malic
Mario Malic 2021 年 2 月 20 日
With this new feature of tagging users in post, I believe some have already turned off such notifications as they are misused as in this question. There are a lot of identical questions, you're not even posting the exact error message.
Most likely, width of R is 3, but you want to access the index that doesn't exist. You can remove semicolon to see the output, or inspect the variable in Workspace.
R = string(data{:});
Please spend few hours in MATLAB Onramp, it helps a lot.
Saud Alfalasi
Saud Alfalasi 2021 年 2 月 20 日
@Mario Malic Thank you for your input. And please don't make asking questions an unpleseant experience - questions should be encouraged, this is how people learn.
Saud Alfalasi
Saud Alfalasi 2021 年 2 月 20 日
@Mario Malic I understand the width of R is 3. However I would like it to be 27 - so if some lines of string from text are missing, It flags up yet my code still runs. The exact error message is in the title
Jan
Jan 2021 年 2 月 21 日
@Saud Alfalasi: "And please don't make asking questions an unpleseant experience - questions should be encouraged, this is how people learn."
I agree with you. When you mention this explicitly I'd like to add: Please keep it pleasent for others to answer questions. Imagine what would happen, if all users catch the attraction of the top ten answerers explicitly for all of their questions. This would be a reason for me to leave the forum to avoid stress.
I answer a question, if I find the time to read it and have an idea about a possible solution. A further pushing decreases your chance to get answers. Therefore Mario's hint is valuable and you can take it as a supoort.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 2 月 20 日

1 投票

data = textscan(fileID,'%s');
fclose(fileID);
text_str = vertcat(data{:});
if length(text_str) > 27; text_str = text_str(1:27); end %in case it was too long
text_str(end+1:27) = {'nil'}; %if it was too short, put in nil

5 件のコメント

Saud Alfalasi
Saud Alfalasi 2021 年 2 月 21 日
Thank you Walter, I think it would be better if i showed an example of the output I'm after:
say my txt file was
apple
pear
bannana
juice
I would want the new array to be:
apple
pear
bannana
nil
nil
juice
nil
nil
nil
ect (until index 27 is reached)
Walter Roberson
Walter Roberson 2021 年 2 月 21 日
data = textscan(fileID,'%s');
fclose(fileID);
text_str = data{1};
mask = cellfun(@isempty, text_str);
text_str(mask) = {'nil'};
Jan
Jan 2021 年 2 月 21 日
Or because it is faster:
mask = cellfun('isempty', text_str);
Saud Alfalasi
Saud Alfalasi 2021 年 2 月 21 日
Records = 'record.txt';
fileID2 = fopen(Records);
data = textscan(fileID2,'%s');
fclose(fileID2);
text_str = data{1:4};
mask = cellfun(@isempty, text_str);
text_str(mask) = {'nil'};
Hi guys, array element 4 doesn't exist,
The output I was expecting is
apple
pear
cheese
nil
However this is the output:
mask =
3×1 logical array
0
0
0
Walter Roberson
Walter Roberson 2021 年 2 月 21 日
You need to switch your method of reading entirely. I suggest that you use readlines() if you have a new enough matlab.

サインインしてコメントする。

カテゴリ

質問済み:

2021 年 2 月 20 日

コメント済み:

2021 年 2 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by