Error creating using xlswrite function
조회 수: 16 (최근 30일)
이전 댓글 표시
Hello all,
I am not an expert in matlab and I am stack right now in a part of my code. The idea is that in my company does not use matlab too much so everything I process in matlab I require to output in excel. '
I work with IFILES from AVL, which are a not common files from combustion analyser software. For these files I use a function developed by matlab:
Normally, I work with a high number of IFILES and therefore, I am trying to automated the script as now I have to do it manually. Right now, I am looping through a folder, acquiring the names of the files, loading these files load_ifile function and saving the variables I am interesting in cell array as IFILE will load to work space as struct. So far, so good. The problem comes when I try to create the name with extension .cvs for create a new cvs files.
The script I have done so far is:
for i=1:2
path='C:\Program Files\MATLAB\R2010a\toolbox\catool';
filePattern=sprintf('*.00%d',i);
files=dir([path '\' filePattern]);
filesnames{i}=files;
%first_name=filesnames{i}.name;
result{i} = load_ifile(filesnames{1,i}.name,1);
for kk=1:4
MFB_5{i,kk}=result{1,i}.(sprintf('AI05_%d',kk)).data';
MFB_10{i,kk}=result{1,i}.(sprintf('AI10_%d',kk)).data';
MFB_50{i,kk}=result{1,i}.(sprintf('AI50_%d',kk)).data';
MFB_90{i,kk}=result{1,i}.(sprintf('AI90_%d',kk)).data';
end
% MFB_combine={MFB_5;MFB_10;MFB_50;MFB_90};
excel_name{i}=cellstr(filesnames{1,i}.name(1:end-4));
Filename{i}=strcat(excel_name{1,i},'.cvs');
xlswrite(Filename{1,i},MFB_5{1,i});
% name=Filename{1,1};
end
Filename is a cell array with the new names for cvs files. I expected to have two cvs files wih the names contained in Filename (that has been previously obtained from the original files). However, it comes out an error that says:
??? Error using ==> xlswrite at 156
Filename must be a string.
Error in ==> test3 at 23
xlswrite(Filename{1,i},MFB_5{1,i});
I am using Matlab R2010a
Any help or guide it would be really appreciated.
Thanks in advanced.
Best regards
댓글 수: 0
채택된 답변
Jan
2018년 12월 28일
편집: Jan
2018년 12월 28일
Some comments:
path='C:\Program Files\MATLAB\R2010a\toolbox\catool';
Do not shadow theportant function path by a local variable, because this can cause serious troubles during debugging.
files=dir([path '\' filePattern]);
Prefer fullfile instead of a horizontal concatenation, because it considers e.g. trailing file separators, when the folder is c:\ .
filesnames{i}=files;
This is at least confusing. files is a struct array, most likely a scalar one, so why do you insert it into a cell array? The "...names" part reduces the clarity also, because filesnames does not contains "names". In addition using the name only without a path will lead to unexpected results, if the path is not the current folder. I recommend:
filesnames{i} = fullfile(path, files(1).name);
result{i} = load_ifile(filesnames{i}, 1);
[folder, file, ext] = fileparts(filesnames{i});
excel_name{i} = fullfile(folder, file);
Filename{i} = strcat(excel_name{i}, '.cvs');
xlswrite(Filename{i}, MFB_5{1, i});
Your problem was, that after
excel_name{i}=cellstr(filesnames{1,i}.name(1:end-4));
the variable excel_name is a cell, which contains a cell string. This is equivalent to:
excel_name = {{'FileName.cvs'}}; % TWO curly braces
Note: Your naming scheme is confusing. Mixing singular and plural, joining the terms or separating them with an undersord, upper/lowercase, "filesnames" for something which is not a list of names, but a struct replied by dir. Simplifying the naming scheme will help to avoid such problems as in your case. A cleaner version:
folder = 'C:\Program Files\MATLAB\R2010a\toolbox\catool';
for iFile = 1:2 % smarter than "i"
filePattern = sprintf('*.00%d', iFile);
fileList = dir(fullfile(folder, filePattern);
fileName{i} = fullfile(folder, file);
result{i} = load_ifile(filesnames{1,i}.name,1);
for kk = 1:4
MFB_5{i,kk} = result{i}.(sprintf('AI05_%d',kk)).data';
MFB_10{i,kk} = result{i}.(sprintf('AI10_%d',kk)).data';
MFB_50{i,kk} = result{i}.(sprintf('AI50_%d',kk)).data';
MFB_90{i,kk} = result{i}.(sprintf('AI90_%d',kk)).data';
end
[~, file] = fileparts(fileName{i});
excelName{i} = fullfile(folder, [file, '.csv']);
xlswrite(excelName{i}, MFB_5{i});
end
Do you really need to collect the results in cell arrays? If so, pre-allocate the arrays before the loop:
fileName = cell(1, 2);
result = cell(1, 2);
... etc.
If you do not use the elements of the cell arrays afterwards, omit the "{i}" indexing.
You are working inside Matlab's installation folder?
path='C:\Program Files\MATLAB\R2010a\toolbox\catool'
This is most likely a bad idea. Prefer to store all used data files, especially if you write into it, to a folder outside the "Program Files" path.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!