How to code MATLAB to call names using strcat
Afficher commentaires plus anciens
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 commentaires
"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.
"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.
Réponses (1)
Dyuman Joshi
le 5 Nov 2023
Modifié(e) : Dyuman Joshi
le 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 commentaires
dpb
le 5 Nov 2023
dataDir='YourWorkingFolderContainingDataFiles'; % set as needed
d=dir(fullfile(dtaDir,'*Skeleton*.csv')); % return matching files
for i=1:numel(d)
data=readmatrix(fullfile(d(i).folder),d(i).name); % read each file in turn
% process each in turn or add to a larger array, whatever..
end
In general, it's abadidea™ to bury metadata in the file names; now you've got to try to extract that from them in order to have the one of interest. Create generic file of SKELETON data and include the id data as variables inside it instead; then you can also use readtable and builtin functions such as rowfun to analyze by grouping variables, etc., etc., .... One can also use splitapply workflow or perhaps grpstats.
NGR MNFD
le 5 Nov 2023
Déplacé(e) : Dyuman Joshi
le 5 Nov 2023
Stephen23
le 5 Nov 2023
"I want to read all the files and put their output in a matrix..."
Sure, that is very easy using DIR.
"but when I run it, even though I am in the same path, it gives me an error similar to the image below."
Because (exactly as the error message tells you) there is no file with that name in the current directory.
Your task would be much easier using DIR.
Dyuman Joshi
le 5 Nov 2023
Modifié(e) : Dyuman Joshi
le 5 Nov 2023
@NGR MNFD, I assume that each folder contains a csv file with the same name.
As Stephen and dpb have suggested, using DIR would be the best approach here -
%% The path to the folder where files are stored
%for you it is -
c = 'C:\Users\HAMSHARI\Documents\MATLAB\movementdisorder';
%Search for all files having the keyword Skeleton in its names with the file
%format .csv, in the above folder
files = dir(fullfile(c, '**\*Skeleton*.csv'));
n = numel(files);
DATA = cell(n,1);
for k=1:n
%Generate the file-path for kth file
str = fullfile(files(k).folder, files(k).name);
%read and store the data from the file
DATA{k} = readmatrix(str);
%% do xyz
end
Though I would say that it is not a good idea to make individual folders and store each file separately in them, but it is what you are working with rn, so I provided an approach accordingly.
Also
> FYI - you can use any keyword which appears in all files e.g. human or normal.
> The 16th line of your code i.e. the dir call does not seem to have any purpose. I'd suggest you to remove it.
Stephen23
le 5 Nov 2023
Note that double asterisk provides a recursive search. A single asterisk will search only the subfolders, which is perfectly sufficient for this use-case:
files = dir(fullfile(c,'*','*Skeleton*.csv'));
Dyuman Joshi
le 5 Nov 2023
@NGR MNFD, try the code I proposed here -
NGR MNFD
le 5 Nov 2023
Walter Roberson
le 5 Nov 2023
According to the error message, the code is trying open human1_norma1_SkeletonData1.csv . However if you look at the display of the current directory, there are only filenames that start with human10_antalgic11
Dyuman Joshi
le 6 Nov 2023
@NGR MNFD, you don't need the for loops in the lines 15-23. Remove them.
Everything was covered in the code I gave.
NGR MNFD
le 6 Nov 2023
NGR MNFD
le 6 Nov 2023
NGR MNFD
le 6 Nov 2023
P = 'C:\Users\HAMSHARI\Documents\MATLAB\movementdisorder';
S = dir(fullfile(P,'human*','human*.csv'));
for k = 1:numel(S)
F = fullfile(S(k).folder, S(k).name);
S(k).data = readmatrix(F);
end
The imported file data will be stored in the structure S. For example, the 2nd file:
S(2).name % filename
S(2).data % filedata
NGR MNFD
le 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.
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

