Error creating using xlswrite function

17 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Jan
Jan il 28 Dic 2018
Modificato: Jan il 28 Dic 2018
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.
  1 Commento
Santos Romero
Santos Romero il 28 Dic 2018
Hello,
It IS WORKING NOW!!!
Thank you for your advice and clarifications. I can say that you know more than I do with matlab. I am really grateful as I have learnt from your explanations and these four-five lines of code I will save me a huge amount of time. THANKS AGAIN!!.
To answer some of your questions, I am saving the elements in cell arrays as I use them afterwards to do some other operations, this is only an small part of the whole script that I am trying to modify in order to automated the process.
I am working inside program files path as I did not manage to make catool works in other way. Maybe I misunderstood the instructions to use the load_ifile function. I will try to make work out of the installation file path.
Last thing, I totally agree that the naming was quite confuse, thanks for the tips. I will try from now on to start with a naming that makes more sense from the beginning instead of doing it afterwards.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by