How to code MATLAB to call names using strcat

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

Stephen23
Stephen23 le 5 Nov 2023
Modifié(e) : Stephen23 le 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
Stephen23 le 5 Nov 2023
Modifié(e) : Stephen23 le 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.

Connectez-vous pour commenter.

Réponses (1)

Dyuman Joshi
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

"Use sprintf to define the name of the file, as it is easier..."
Using dir is even easier yet...
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
NGR MNFD le 5 Nov 2023
Déplacé(e) : Dyuman Joshi le 5 Nov 2023
Tnx Dear,I want to read all the files and put their output in a matrix, but when I run it, even though I am in the same path, it gives me an error similar to the image below.
Stephen23
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
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.
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'));
NGR MNFD
NGR MNFD le 5 Nov 2023
Modifié(e) : NGR MNFD le 5 Nov 2023
Tanx everybody , I can't understand at all and I'm confused. I want to have 20 folders with different normal names forexample(human1(1:10)_normal1(1:20)_SkeletonData(1:6)), each folder contains six .csv files.(In other words, I have 20*10*6=1200 files that I need to read). I executed these codes according to your instructions, but it still gives an error. what is the problem?
NGR MNFD
NGR MNFD le 5 Nov 2023
tanx, I executed your code exactly, but from the beginning the problem was somewhere else, it gets stuck in the path and cannot open the files, pay attention to the error. (I also created 1200 zero files and placed them after line 14, but it didn't matter(
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
@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
NGR MNFD le 6 Nov 2023
Hello, I think you didn't pay attention to the pictures I sent. I typed your code exactly. It gets stuck in the path of my files and doesn't find the folders. It has nothing to do with this code and the strcat code. Please pay attention to the error.
(I put my MATLAB coding script in the data folder, when I run the program, the folders are open, but I don't know why it doesn't open them.
According to the picture, I have several files, each of which contains six files with the extension .csv. I want it to take these out of the folders and read them for me and put them in a matrix one after the other and save them.)
Error using readmatrix (line 158)
Unable to find or open 'human1_normal1_SkeletonData1.csv'. Check
the path and filename or file permissions.
Error in patologicaldata (line 21)
data = readmatrix(str);
Stephen23
Stephen23 le 6 Nov 2023
@NGR MNFD: please paste the exact code you are using in a new comment. As text, not a screenshot.
NGR MNFD
NGR MNFD le 6 Nov 2023
c = 'C:\Users\HAMSHARI\Documents\MATLAB\movementdisorder';
files = dir(fullfile(c,'*','*Skeleton*.csv'));
n = numel(files);
DATA = cell(n,1);
for k=1:n
str = fullfile(files(k).folder, files(k).name);
DATA{k} = readmatrix(str);
end
for i= 1:10
for j = 1:20
for k = 1:6
str = sprintf('human%d_normal%d_SkeletonData%d.csv', [i j k]);
data = readmatrix(str);
%do xyz
end
end
end
NGR MNFD
NGR MNFD le 6 Nov 2023
I don't know what the codes that I wrote before the for loop are and I added them to your suggestion
Stephen23
Stephen23 le 6 Nov 2023
Modifié(e) : Stephen23 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
NGR MNFD le 6 Nov 2023
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 le 6 Nov 2023
Modifié(e) : Stephen23 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Produits

Version

R2021a

Modifié(e) :

le 6 Nov 2023

Community Treasure Hunt

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

Start Hunting!

Translated by