Re: Cells and saving as ASCII
- From: "Anandhi " <anandhi@xxxxxxxxxxxxx>
- Date: Mon, 6 Jul 2009 14:01:04 +0000 (UTC)
Hi,
i used this method to save text and numeric data to a ascii tab delimited file. I may be useful to someone.
---------code----
heading={'col1'
'col2'
'col3'};
aa=[1 2 3; 4 5 6; 7 8 9];
fid=fopen('write_tablimited.txt','wt');
fprintf(fid,'%s\t%s\t%s\t\n',heading{:});
fprintf(fid,'%i\t%i\t%i\t\n',aa);
fclose(fid);
anandhi
"Corinna Schmitt" <csc@xxxxxxxxxxxxx> wrote in message <f8q4ua$rv3$1@xxxxxxxxxxxxxxxxxx>...
HAllo,.
thanks for the help. I modified your answer as needed:
%store as ASCII-format
fid=fopen('aidaForMchips.txt','wt');
for i=1:size(finalTable,1),
fprintf(fid,'%s %s %s %s %s %s %s %s %s \n',finalTable{i,:});
end
fclose(fid);
How can I create now a Tab-delimited ASCII-file. My idea was: fprintf(fid,'%s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \n',finalTable{i,:});
But it does not work.
Any idea?
Thanks, Corinna
"Gautam Vallabha" <gvallabh@xxxxxxxxxxxxx> wrote in message <f8kq23$b80$1@xxxxxxxxxxxxxxxxxx>...
"Corinna Schmitt" <csc@xxxxxxxxxxxxxxxxx> wrote in message
news:ef5f16c.-1@xxxxxxxxxxxxxxxxxxxxxxxxxx
I have to work with cells at the moment. It consists of 9 columns and[snip]
2049 rows. In the cells there are stored test and number information
like the followed:
1 1 1 1 0 AR_WIZ 560.0 0.0 01_01_01_01
1 1 3 1 0 CTNNB1_WIZ 1007.0 0.0 01_01_03_01
The columns 6 and 9 content text-information and the others should be
numbers.
1. How can I be sure that the columns 1-5 and 7,8 stores the
information as numbers?
There is no "automatic" way to force the columns to be numeric. However,
before processing and saving the data you can check the data validity and
convert if needed.
% first define two m-functions
%--------------------------
function dd = ensureNumericColumns(dd)
idx = find(cellfun(@ischar, dd)); % get any strings
dd(idx) = num2cell(str2double( dd(idx) )); % convert
%--------------------------
function dd = ensureStringColumns(dd)
idx = find(cellfun(@isnumeric, dd)); % get any numbers
dd(idx) = cellfun( @num2str, dd(idx), ...
'UniformOutput', false );
%--------------------------
% now clean up your data
d = cell(2048,9); % your data
d(:,[1:5 7:8]) = ensureNumericColumns( d(:,[1:5 7:8]) );
d(:,[6 9]) = ensureStringColumns( d(:,[6 9]) );
2. How can I delete whole columns or/and rows in a cell?
Assign them to []:
d(152,:) = []; % delete a row
d(:,2) = []; % delete column 2
3. How can I store the whole information in an ASCII file?
The result file must be in ASCII-format. But at the moment I'm just
able to store it as an xls file:
------------
fid = fopen('myfile.txt', 'w');
for i=1:size(d,1), % for each row
fprintf(fid, '%g %g %g %g %g %s %g %g %s', d{i,:});
end
fclose(fid);
------------
See the help for FPRINTF to tweak the output format as needed:
--
Gautam Vallabha
The MathWorks
Email: Gautam.Vallabha@xxxxxxxxxxxxx
- Prev by Date: Re: How to select Matlab version
- Next by Date: Re: Varibale in plot title returning an error
- Previous by thread: system time in matlab GUI
- Next by thread: dir
- Index(es):
Relevant Pages
|
Loading