Concatenate variables by searching mkdir folder and adding to string, n+1

Hello!
I am working on a script that has a few different calculations being done. Each one produces its own figure which is then saved into a folder using mkdir(). I would like to have the user only have to enter in the ID name and have the script auto-generate the names of the figures. Some ID folders already have figures already stores in them, so I want the code to do an 'n+1' process where it has the ID name and adds a number to the end. The number being the next available in order.
For example:
ID=Jerry
Circle_image_name = strcat('ID', 'Circle','n+1')
Energy_Image_Name = strcat('ID', 'Energy','n+1')
I am not sure how to create a loop that reads the files and determines what 'n' is equal to. I then need to use 'n' and insert it into a string and concatenate it to name the figures.
Here is the script:
filenamestr = inputdlg('Enter ID Number', 's'); %Prompts user to enter ID Number
ID_Name = filenamestr{:}; %Saves ID name as string
mkdir ('C:\Users\User\Desktop\Data\', ID_Name); %Uses string to create/use folder
CircFig=heatmap(Circle) %Creates circle figure
CircStr = inputdlg('Enter Circle Image Name', 's'); %Prompts user to enter circle name
Circle_Image_Name = CircStr{:}; %Stores circle name as string
saveas(CircFig, fullfile( 'C:\', 'Users', 'User', 'Desktop', 'Data', ID_Name, Circle_Image_Name), 'jpeg');
mapE = heatmap(Energy) %Creates energy figure
EnergyStr = inputdlg('Enter Energy Image Name', 's'); %Prompts user to enter energy name
Energy_Image_Name = EnergyStr{:}; %Stores energy name as string
saveas(mapE, fullfile( 'C:\', 'Users', 'User', 'Desktop', 'Data', ID_Name, Energy_Image_Name), 'jpeg');

 Accepted Answer

Create a dedicated function for this job:
BaseFolder = fullfile( 'C:\', 'Users', 'User', 'Desktop', 'Data');
...
Circle_Image_Name = CircStr{:};
FileName = GetNumberedName(fullfile(BaseFolder, ID_Name), Circle_Image_Name, 'jpeg');
saveas(CircFig, FileName);
...
function FileName = GetNumberedName(Folder, Name, Ext)
FileList = dir(fullfile(Folder, [Name, '*.', Ext]);
Num = numel(FileList) + 1;
FileName = fullfile(Folder, sprintf('%s%d.%s', Name, Num, Ext));
while isfile(FileName) % Check, if this file is really not existing:
Num = Num + 1;
FileName = fullfile(Folder, sprintf('%s%d.%s', Name, Num, Ext));
end
end
It is saver to check the existence of a file at first. Maybe you have used the folder before already and a file has been deleted:
Folder\File1.jpeg % File2 was delete before
Folder\File3.jpeg
Then simply counting the number of files would produce a collision.
By the way, prefer leading zeros for indexed file names. Then the alphabetcial order is the numerical order also:
sprintf('%s%05d.%s', Name, Num, Ext)

4 Comments

Error: File: try1.m Line: 158 Column: 51
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Line 158 is the FileList = dir(fullfile(Folder, [Name, '*.', Ext]);
What could be causing this? Do I need to change 'Folder' to something else? Is it the order I have it in?
Here is the full code.
I also have another section a hundered lines down, should it be placed below/before?
filenamestr = inputdlg('Enter ID name', 's'); %Box pops up for ID input
ID_Name = filenamestr{:}; %Takes input and makes a string
mkdir ('C:\Users\User\Desktop\Patient_Data\', ID_Name); %Attaches created file name to path
CircStr = inputdlg('Enter Circulation Image Name', 's'); %Box pops up for Circ input
Circle_Image_Name = CircStr{:}; %Takes input and makes a string
saveas(CircFig, fullfile( 'C:\', 'Users', 'User', 'Desktop', 'Patient_Data', ID_Name, Circle_Image_Name), 'jpeg'); %Creates/uses folder to save image
BaseFolder = fullfile( 'C:\', 'Users', 'User', 'Desktop', 'Data');
Circle_Image_Name = CircStr{:};
FileName = GetNumberedName(fullfile(BaseFolder, ID_Name), Circle_Image_Name, 'jpeg');
saveas(CircFig, FileName);
function FileName = GetNumberedName(Folder, Name, Ext)
FileList = dir(fullfile(Folder, [Name, '*.', Ext]);
Num = numel(FileList) + 1;
FileName = fullfile(Folder, sprintf('%s%d.%s', Name, Num, Ext));
while isfile(FileName) % Check, if this file is really not existing:
Num = Num + 1;
FileName = fullfile(Folder, sprintf('%s%d.%s', Name, Num, Ext));
end
end
Other than that, Jan you are a wizard my friend. Respect.
I'm a programmer :-) I'm glad if this helps you.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 24 Mar 2021

Commented:

Jan
on 25 Mar 2021

Community Treasure Hunt

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

Start Hunting!