Borrar filtros
Borrar filtros

Why only first character of string is writing into .csv file

4 visualizaciones (últimos 30 días)
In my project i would like to write strings form array to .csv file. But it can writes only first character of the string.
my code is
clc
n=1;
Fe={'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q';'R';'S';'T';'U';'V';'W';'X';'Y';'Z';'AA';'AB';'AC';'AD';'AE';'AF';'AG';'AH';'AI';'AJ';'AK';'AL';'AM';'AN';'AO';'AP';'AQ';'AR';'AS';'AT';'AU';'AV';'AW';'AX';'AY';'AZ';'BA';'BB';'BC';'BD';'BE';'BF';'BG';'BH';'BI';'BJ';'BK';'BL';'BM';'BN';'BO';'BP';'BQ';'BR';'BS';'BT';'BU';'BV';'BW';'BX';'BY';'BZ';'CA';'BB';'CC';'CD';'CE';'CF';'CG';'CH';'CI';'CJ';'CK';'CL';'CM';'CN';'CO';'CP';'CQ';'CR';'CS';'CT';'CU';'CV';'CW';'CX';'CY';'CZ';'DA';'DB';'DC';'DD';'DE';'DF';'DG';'DH';'DI';'DJ';'DK';'DL';'DM';'DN';'DO';'DP';'DQ';'DR';'DS';'DT';'DU';'DV';'DW';'DX';'DY';'DZ';'EA';'EB';'EC';'ED';'EE';'EF';'EG';'EH';'EI';'EJ';'EK';'EL';'EM';'EN';'EO';'EP';'EQ';'ER';'ES';'ET';'EU';'EV';'EW';'EX';'EY';'EZ';'FA';'FB';'FC';'FD';'FE';'FF';'FG';'FH';'FI';'FJ'};
Fa= {'AA','AB','AC'};
fn='1_LET.csv';
n=n+1;
for i=25:30
for j=1:1
cellRef_0 = sprintf('A%d:A%d',n,n);
cellRef1 = sprintf('B%d:CH%d',n,n);
cellRef2 = sprintf('CI%d:EK%d',n,n);
x1 = imread(sprintf('D:/img/%d/%d.bmp',i,j));
xlswrite(fn,Fe{i},cellRef_0);
feature_extractor(x1);
xlswrite(fn,ans',cellRef1);
feature_extractor_2d(x1);
xlswrite(fn,ans',cellRef2);
n=n+1;
end
fprintf( 'completed of %d\n',i );
end
  1 comentario
Stephen23
Stephen23 el 21 de Sept. de 2016
Editada: Stephen23 el 21 de Sept. de 2016
You should learn to pay attention to the warnings in the MATLAB code editor:
What does ans refer to? It is not clear at all from your code. It makes it impossible for us to test your code.
Also the indentation is inconsistent, making it hard to follow the code logic. When you use consistent code indentation then it makes it easier to understand the code, which makes it easier to write code without bugs.

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 21 de Sept. de 2016
As per Stephen's comment do not use the automatically created ans variable| to get to the output of functions in your code. This is very bad practice and will results in some hard to track bugs. Assign the return value explicitly to a variable:
features = feature_extractor(x1);
xlswrite(fn, features', cellRef1);
features = feature_extractor_2d(x1);
xlswrite(fn, features',cellRef2);
Now, to answer your question, I assume the problem is with xlswrite(fn,Fe{i},cellRef_0), when Fe{i} is more than one character. The cause of the problem is that Fe{i} is a matrix and matlab writes each element of a matrix (hence each character) to a different excel cell. It is only when Fe{i} is a cell array of strings that matlab writes each string into its own cell. The solution is simple, pass Fe{i} as a cell array, Fe(i) instead of a matrix, so:
xlswrite(fn, Fe(i), cellRef_0);
will fix the issue.

Más respuestas (0)

Categorías

Más información sobre Data Import from MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by