- I use tabs in text files only when tabs are explicitly required. How the text displays depends on the tab-positions of the program, in which the text is displayed.
- Combinations of left and right justified tabs is just too tricky.
- The function may be further simplified and improved
Index exceeds matrix dimensions when writing to text file?
1 次查看(过去 30 天)
显示 更早的评论
uData is 27x5 matrix containing numbers.
name_Database is a 27x1 cell array containing names in the form of strings.
I am getting the error
Index exceeds matrix dimensions.
Where am I going wrong? Input here would be greatly appreciated.
% Header 'Name', 'ID', 'scE', 'ccE', 'scC', 'ccC'
ID = dbedit.uData(:,1);
scE = dbedit.uData(:,2);
ccE = dbedit.uData(:,3);
scC = dbedit.uData(:,4);
ccC = dbedit.uData(:,5);
names = dbedit.name_Database;
% Create array versions to account for proper spacing
% within the text file.
ID_cell = cellstr(num2str(ID.'));
scE_cell = cellstr(num2str(scE.'));
ccE_cell = cellstr(num2str(ccE.'));
scC_cell = cellstr(num2str(scC.'));
ccC_cell = cellstr(num2str(ccC.'));
output_file = 'uDatabase.txt'; % Text file to output data into.
fid = fopen(output_file, 'w+'); %// open file for writing
fprintf(fid, 'Name\t ID\t scE\t ccE\t scC\t ccC\n'); % Header
for ii=1:numel(names)
fprintf(fid, '%s\t %s\t %s\t %s\t %s\t %s\n',names{ii},...
ID_cell{ii},scE_cell{ii},ccE_cell{ii},scC_cell{ii},...
ccC_cell{ii}); %// write data
end
fclose(fid);
0 个评论
回答(1 个)
per isakson
2014-12-19
编辑:per isakson
2014-12-19
How should the output file to look like?
'%s\t %s\t %s\t %s\t %s\t %s\n'   Why both tab and space?
The conversion cellstr(num2str( ... )) is not needed. fprintf can output "proper spacing within the text file" directly (with the proper format string).
These lines don't do what you expect (I guess)
ID_cell = cellstr(num2str(ID.'));
scE_cell = cellstr(num2str(scE.'));
ccE_cell = cellstr(num2str(ccE.'));
scC_cell = cellstr(num2str(scC.'));
ccC_cell = cellstr(num2str(ccC.'));
Here are some links on debugging in Matlab
 
Run cssm, which produce a text file containing
Name ID scE ccE scC ccC
Name01 0.11 0.79 0.14 0.61 0.04
Name02 0.59 0.77 0.90 0.81 0.49
Name03 0.89 0.66 0.43 0.03 0.29
Name04 0.90 0.39 0.56 0.62 0.06
Name05 0.54 0.24 0.01 0.36 0.70
Name06 0.76 0.12 0.48 0.14 0.32
....
where
function cssm( )
uData = rand( 27, 5 );
names = arrayfun(@(jj) sprintf('Name%02d',jj),[1:27],'uni',false );
% Header 'Name', 'ID', 'scE', 'ccE', 'scC', 'ccC'
ID = uData(:,1);
scE = uData(:,2);
ccE = uData(:,3);
scC = uData(:,4);
ccC = uData(:,5);
output_file = 'uDatabase.txt'; % Text file to output data into.
fid = fopen(output_file, 'w+'); %// open file for writing
fprintf(fid, 'Name\t ID\t scE\t ccE\t scC\t ccC\n'); % Header
for ii=1:numel(names)
fprintf(fid,'%s\t %5.2f\t %5.2f\t %5.2f\t %5.2f\t %5.2f\n' ...
, names{ii}, ID(ii), scE(ii), ccE(ii), scC(ii), cC(ii) );
end
fclose(fid);
end
 
This is probably not what you want. Next step is to modify the format string appropriately.
 
I modified the function, cssm.m. Now it outputs:
Name ID scE ccE scC ccC
Subject 1 5 9 9 4 9
Subject 2 4 4 6 7 7
....
Subject27 8 1 8 5 6
where
function cssm( )
uData = randi( [1,9], [27,5] );
names = arrayfun(@(jj) sprintf('Subject%2d',jj),[1:27],'uni',false);
colhead = { 'Name', 'ID', 'scE', 'ccE', 'scC', 'ccC' };
ID = uData(:,1);
scE = uData(:,2);
ccE = uData(:,3);
scC = uData(:,4);
ccC = uData(:,5);
output_file = 'uDatabase.txt'; % Text file to output data into.
fid = fopen(output_file, 'w+');
fprintf(fid, '%-9s%6s%6s%6s%6s%6s\n', colhead{:} ); % column headers
for ii = 1 : numel( names )
fprintf(fid, '%-9s%6d%6d%6d%6d%6d\n' ...
, names{ii}, ID(ii), scE(ii), ccE(ii), scC(ii), ccC(ii) );
end
fclose(fid);
end
 
Comments
function cssm( )
uData = randi( [1,9], [27,5] );
names = arrayfun(@(jj) sprintf('Subject%2d',jj),[1:27],'uni',false);
colhd = { 'Name', 'ID', 'scE', 'ccE', 'scC', 'ccC' };
output_file = 'uDatabase.txt'; % Text file to output data into.
colhd_frmt = '%-9s%6s%6s%6s%6s%6s\n';
data_frmt = '%-9s%6d%6d%6d%6d%6d\n';
fid = fopen( output_file, 'w+' );
fprintf( fid, colhd_frmt, colhd{:} ); % column headers
for ii = 1 : numel( names )
fprintf( fid, data_frmt, names{ii}, uData(ii,:) );
end
fclose(fid);
end
2 个评论
per isakson
2014-12-19
I modified the function and added to my answer. Note the similarity between the two format strings. It illustrates the way I prefer.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!