How to code MATLAB to call names using strcat
4 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 5 Nov 2023
Edited: Stephen23
on 5 Nov 2023
"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
on 5 Nov 2023
Edited: Stephen23
on 6 Nov 2023
"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.
Answers (1)
Dyuman Joshi
on 5 Nov 2023
Edited: Dyuman Joshi
on 5 Nov 2023
"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 Comments
Stephen23
on 6 Nov 2023
Edited: Stephen23
on 6 Nov 2023
"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.
See Also
Categories
Find more on File Operations 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!