フィルターのクリア

How to code MATLAB to call names using strcat

8 ビュー (過去 30 日間)
NGR MNFD
NGR MNFD 2023 年 11 月 5 日
編集済み: Stephen23 2023 年 11 月 6 日
My data has such a name like person1_normal1_Data2, that The numbers 1 and 2 inside the names vary from 1 to 20. The extension of my files is .csv. There are several files, how can I call it and put it in a matrix? Even though I typed such a code, at first it gets stuck in my path, where is the problem? Please advise. Thankful
code matlab
dir C:\Users\HAMSHAHRI\Documents\MATLAB\movementdisorder\human10_normal17_SkeletonData
% data= readmatrix('human10_normal17_SkeletonData1.csv');
for i=10
for j=17
for k=1
data1 = load(strcat(strcat(strcat(strcat(strcat(strcat('human',num2str(i)),'_'),....
'normal',num2str(j)),'_'),'SkeletonData',num2str(k))));
DATA1 = [DATA1;data1];
end
end
end
  2 件のコメント
Stephen23
Stephen23 2023 年 11 月 5 日
編集済み: Stephen23 2023 年 11 月 5 日
"The extension of my files is .csv"
Tha sounds like a very easy task with DIR.
"There are several files, how can I call it and put it in a matrix?"
Use indexing:
"Even though I typed such a code, at first it gets stuck in my path, where is the problem?"
What does "stuck in my path" mean exactly?
I presume that these FOR loops are only for testing purposes:
for i=10
for j=17
for k=1
Why do you need to nest STRCAT six times? Why not just call it once? Or even better, replace it with SPRINTF.
Stephen23
Stephen23 2023 年 11 月 5 日
編集済み: Stephen23 2023 年 11 月 6 日
"it gives me an error about the dir command. Where is the problem?"
The point of using DIR is that you can get rid of that complex filename generation (which does not work) for something much simpler (that simply loops over the names of any existing files). See the DIR example here:
"While I am in its own folder"
That is not required, nor should you combine folders of data with code.
I note that in your screenshot your current directory does not have any CSV files in it (but it does contain what appears to be many subfolders with names matching the CSV files that you claim you want to import). This makes the information you have provided inconsistent, which makes it harder and slower for us to help you with a working solution.

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

回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 5 日
編集済み: Dyuman Joshi 2023 年 11 月 5 日
"Even though I typed such a code, at first it gets stuck in my path, where is the problem?"
Probably because you have not added the format of the file in the string.
Use sprintf to define the name of the file, as it is easier and robust, and use readmatrix instead of load() -
for i= range_of_numbers_after_human
for j = range_of_numbers_after_normal
for k = range_of_numbers_after_SkeletonData
str = sprintf('human%d_normal%d_SkeletonData%d.csv', [i j k]);
data = readmatrix(str);
%do xyz
end
end
end
Also, it would be better to preallocate the output array - Preallocate arrays
  17 件のコメント
NGR MNFD
NGR MNFD 2023 年 11 月 6 日
I used exactly the same command and got the same error.
S(2).data
Unrecognized field name "data".
K>> S(2).human10_antalgic5_SkeletonData
Unrecognized field name "human10_antalgic5_SkeletonData".
Stephen23
Stephen23 2023 年 11 月 6 日
編集済み: Stephen23 2023 年 11 月 6 日
"S(2).data .. Unrecognized field name "data"."
Very odd: if DIR returned multiple elements in S then the loop should have iterated and the field DATA should exist. Clear the workspace, run the code again, and then show the outputs of these commands:
size(S)
fieldnames(S)
"S(2).human10_antalgic5_SkeletonData"
It is unclear what you expect that to achieve: nothing in my code creates a field with that name.

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

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by