Array in loop - Index exceeds the number of array elements (3) - problem
1 view (last 30 days)
Show older comments
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 Comments
Jan
on 21 Feb 2021
@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.
Answers (1)
Walter Roberson
on 20 Feb 2021
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 Comments
Walter Roberson
on 21 Feb 2021
You need to switch your method of reading entirely. I suggest that you use readlines() if you have a new enough matlab.
See Also
Categories
Find more on Matrix Indexing 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!