Get variable name into filename using strcat

5 ビュー (過去 30 日間)
Louise Wilson
Louise Wilson 2019 年 8 月 26 日
コメント済み: Walter Roberson 2019 年 8 月 27 日
I am trying to create a filename for a .csv file using an existing variable name, which is a number e.g. '5100', and a manual input, e.g. 'files'.
My code:
x=sum(A(:,2:end),2); %column vector containing the sum of each row (from columns 2:end)
short_files=A(find(x==0),1); %get datenum corresponding to each row of zeros
formatOut='yymmddHHMMSS'; %convert datenum back to original filename format
short_files=datestr(short_files, formatOut);
filename_shortfiles=('short_files.csv'); %.csv filename
serialNo='5100';
%try to get serial no into filename :-S
%filename_shortfiles=strcat(serialNo,'short_files.csv'); %.csv filename
dlmwrite(fullfile(path,filename_shortfiles),short_files,'delimiter','');
Here, A is a matrix of data. Within that, I find the rows that contain zeros, and take the value in the first column of those particular rows. It is these values which I put in my output file that I am trying to write to a .csv file.
If I try
filename_shortfiles=('short_files.csv');
...this works, but I would like to have part of the filename change on each iteration of the loop, depending on the value of serialNo.
The value of serialNo changes on each iteration of an earlier part of my code...
serialNo is a 1x1 double, is this the problem? I tried converting it using char() but this didn't work.
Thanks!
  3 件のコメント
Louise Wilson
Louise Wilson 2019 年 8 月 27 日
Thank you Stephen, that makes sense. Can you please point to where I am doing this? I'm confused because I think I only use path here:
dlmwrite(fullfile(path,filename_shortfiles),short_files,'delimiter','');
and this is using the path to the folder where I want to write the files?
Walter Roberson
Walter Roberson 2019 年 8 月 27 日
The problem is in naming your variable "path" . Name your variable something else.
If you are trying to refer to the current directory, then you should use cd()

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 8 月 26 日
Nowhere do you describe/show what the output filename should look like, but this should get you started:
>> sno = 5100;
>> inp = 'short_files.csv';
>> [~,fnm,ext] = fileparts(inp);
>> out = sprintf('%s_%d%s',fnm,sno,ext)
out = short_files_5100.csv

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by