How to resolve error using save and output file copy titles?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I'm trying to perform the following code but I keep getting an error using save. The code is supposed to load the filename and data for .txt files in a specified folder and shuffle the data from each .txt file using a SmallShuffle function to create the variable SS_data. It's then supposed to save multiple output files (18 copies plus the original filename copy) where each file contains one single column of shuffled data. The output files are supposed to be saved as the original filename plus the copy number...(ie. if the original file was called myfile.txt the output files should be myfile1.txt, myfile2.txt,...., myfile18.txt). Right now the non-command format works, but it only gives a single output file titled "outputfile". I have also tried using the command format: save (outputfile ,'SS_data','-ascii'); but it gives me an error "must be a string scalar or character vector". How could I resolve the output title problem and errors associated with save?
directory_name = uigetdir(pwd,'Select data directory');
directory_name = ([directory_name '/']);
files = dir([directory_name,'*txt']);
if isempty(files)
msgbox('No raw files in this directory')
end
FileName=[];
for i=1:length(files)
for j=1:18
filename=files(i).name;
data = load(filename,'-ascii');
filename=filename(1:end-4);
FileName=[FileName; cellstr(filename)];
outputfile = strcat(FileName,num2str(j),'.txt');
A=1;
SS_data=SmallShuffle(data,A);
save outputfile SS_data -ascii;
end
end
댓글 수: 0
채택된 답변
Geoff Hayes
2019년 4월 13일
Lauren - it looks like your FileName is a cell array instead of a string...this may be the source of your error. It's not clear to me why you are doing
FileName=[FileName; cellstr(filename)];
which is creating the cell array. Instead, consider something like
for i=1:length(files)
for j=1:18
filename=files(i).name;
data = load(filename,'-ascii');
outputfile= fullfile(directory_name, sprintf('%s%d.txt',filename(1:end-4), j));
A=1;
SS_data=SmallShuffle(data,A);
save(outputfile, 'SS_data', '-ascii');
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 LaTeX에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!