Same code line works outside but not inside for loop.
古いコメントを表示
I have a code which i don't understand why acts differently outside compared to inside a for loop.
With this code:
filename = ('elspot-prices_2019_hourly_nok – Kopi.xlsx');
[~,~,raw] = xlsread(filename);
hourValues{1} = raw(any(strcmp(raw(:,2),["00 - 01"])>0,2),14);
hourValues{2} = raw(any(strcmp(raw(:,2),["01 - 02"])>0,2),14);
hourValues{3} = raw(any(strcmp(raw(:,2),["02 - 03"])>0,2),14);
I'll get the result: hourValues 1x3 cell, where each cell have an array of hour values.
I need this for 24 hours, so a loop would be great. But when like this:
for i=0:23
if i < 10
text1 = "0" + string(i);
else
text1 = string(i);
end
if i+1 < 10
text2 = "0" + string(i+1);
else if i+1 == 24
text2 = "00";
else
text2 = string(i+1);
end
end
fulltext = text1 + " - " + text2;
hourValues{i+1} = raw(any(strcmp(raw(:,2),[fulltext])>0,2),14); %<--Why don't this line work?
end
i'll get: hourValues 1x24 cell which all is empty. Just showing [] in each of the cells.
(I've tried all kinds of combination for variabel fulltext, with and without [ ], and also both as string or char.)
Any idea why this don't work?
(Edit: xlsx file added)
5 件のコメント
Benjamin Großmann
2021 年 5 月 3 日
Please upload the corresponding xlsx file.
Scott MacKenzie
2021 年 5 月 3 日
Yes, including the xlsx file would help.
I do see some issues in your code, however.
If you want to pad a string left with zeros, you don't need if-else statements. For example, you can replace
if i < 10
text1 = "0" + string(i);
else
text1 = string(i);
end
with
text1 = sprintf('%02s', string(i));
dpb
2021 年 5 月 3 日
Without a copy of the file, it's hard to say -- but that looks like very convoluted way to approach the problem from the git-go.
I'd recommend use readtable, converting the date strings to datetime and then process; much simpler to do comparisons than text/string processing.
Attach a sample input file...
Rudolf
2021 年 5 月 3 日
Rudolf
2021 年 5 月 3 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!