How to set the output file name same as input names but with an additional word

7 次查看(过去 30 天)
I have a few folders each with one .txt file. I load one file at a time using a Matlab GUI:
function load_data_callback(~,~,h)
[FileName, FilePath, FlagUp]=uigetfile({'*.txt'; '*.csv'; '*.xls'; '*.xlsx'; '*.*'}, 'Choose Tab-Delimited Data File');
and make the calculations. Then, the function makes an outputfile called 'NumberOfSpikesPerROI_JCK.csv' by using:
C = ([SeriesLabels;num2cell(SeriesData)]);
writecell(C,'NumberOfSpikesPerROI_JCK.csv')
I want to get the name of the outputfile based on the name of the input file.
For example, if the file name of the first file that I loaded is 'MyFirstFile', then, the output should be 'MyFirstFile_NumberOfSpikesPerROI_JCK.csv' and saved in the same folder (which is currently in Matlab path) as the input file.
Please suggest how to achieve this.
Thank you.

采纳的回答

the cyclist
the cyclist 2022-12-27
If I understand your code correctly, then you've got
FileName = 'MyFirstFile';
So, you could get your new filename as
newFileName = [FileName, '_NumberOfSpikesPerROI_JCK.csv'] % Use square brackets to concatenate character arrays
newFileName = 'MyFirstFile_NumberOfSpikesPerROI_JCK.csv'
It's just a little bit trickier if your input filename is actually something like
FileName = 'MyFirstFile.csv'
and you are trying to insert the text in the middle. But, the principle is the same. Please clarify, if I have misunderstood your question.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by