필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
sukhesh chukkapalli
sukhesh chukkapalli 2016년 9월 21일
댓글: sukhesh chukkapalli 2016년 9월 21일
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
Stephen23
Stephen23 2016년 9월 21일
편집: Stephen23 2016년 9월 21일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 9월 21일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by