Hi all, i create a very big file in matlab and then i try to write it in a txt file using the fprintf() function. But after a while a message pops up saying that there is not enough memory or sth like that.. How can i save it in the txt file? Can i use the save function of matlab to do that?
thanks. Daniel

댓글 수: 12

dpb
dpb 2014년 6월 19일
편집: dpb 2014년 6월 19일
How big is "very big"???
We need exact, complete error messages in context, not cute snippets of "or sth like that". Even with all the talents of forum denizens, the Mind Reading Toolbox isn't widely available.
Show the code snippet that tried to write the file and the resulting error cut 'n pasted from the command window without edits.
Why would you choose to save a large file as text, anyway? Surely you wouldn't expect/need a user to look at it manually that way?
Use save and save it as a .mat file instead.
José-Luis
José-Luis 2014년 6월 19일
편집: José-Luis 2014년 6월 19일
How big is your file? Have you tried reading the documentation. Also, please accept an answer once it has solved your problems.
A text file is bound the be larger than a .mat file.
Daniel Barzegar
Daniel Barzegar 2014년 6월 19일
it's 60000x16.. i need to have in .txt file for later use in other softwares.
José-Luis
José-Luis 2014년 6월 19일
That's really not very big. What commands are you using?
Daniel Barzegar
Daniel Barzegar 2014년 6월 19일
i m using fprintf() to store it in a txt file
Daniel Barzegar
Daniel Barzegar 2014년 6월 19일
i also use the timeseries.resample function as i want to resample my input files first. So i also do the resample() function in 5 files 60000X3 before i merge them all together to get the 60000X15+1
Daniel Barzegar
Daniel Barzegar 2014년 6월 19일
error: Index exceeds matrix dimensions.
this is the error that pops up showing the line in my script where i do the fprintf().
a = 1:3;
your_data = a(4)
You are probably trying to do something like that. You are trying to access something that does not exist.
Please try using the debugger and look at the offending variables. The error should become evident.
Image Analyst
Image Analyst 2014년 6월 19일
Daniel, it's like pulling teeth. Where is your code or the FULL error message, or both? Why won't you let us help you by providing it?????????????????
dAcc=load('C:\Users\cs071372\Desktop\dataRec\Damy\00acc_1401966104620.txt');
dGyro=load('C:\Users\cs071372\Desktop\dataRec\Damy\00gyro_1401966104622.txt');
dMagn=load('C:\Users\cs071372\Desktop\dataRec\Damy\00Magn_1401966104625.txt');
dOr=load('C:\Users\cs071372\Desktop\dataRec\Damy\00Or_1401966104626.txt');
dRotQuatr=load('C:\Users\cs071372\Desktop\dataRec\Damy\00RotQuart_1401966104623.txt');
%acc
tAcc=timeseries;
tAcc.data = dAcc(1:end,3:end);
%gyro
tGyro=timeseries;
tGyro.data = dGyro(1:end,3:end);
%Magn
tMagn=timeseries;
tMagn.data = dMagn(1:end,3:end);
%Or
tOr=timeseries;
tOr.data = dOr(1:end,3:end);
%RotQuatr
tRotQuatr=timeseries;
tRotQuatr.data = dRotQuatr(1:end,3:end);
minim = min(dAcc(1:end, 2));
maxim = max(dAcc(1:end, 2));
starting = (dAcc(1 , 2) - minim); % = 0
ending = (maxim - minim)*power(10,-9);
tAcc.time = (dAcc(1:end, 2)-minim)*power(10,-9) ;
res_tAcc = resample(tAcc, starting:0.0025:ending,'zoh');
res_tGyro = resample(tGyro, starting:0.0025:ending,'zoh');
res_tMagn = resample(tMagn, starting:0.0025:ending,'zoh');
res_tOr = resample(tOr, starting:0.0025:ending,'zoh');
res_tRotQuatr = resample(tRotQuatr, starting:0.0025:ending,'zoh');
fid = fopen('damyResample.txt', 'wt+');
for i=1:size(res_tAcc.data,1)
fprintf(fid, '%f ', res_tAcc.time(i,1));
fprintf(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f', res_tAcc.data(i,1)', res_tAcc.data(i,2)', res_tAcc.data(i,3)',res_tGyro.data(i,1)', res_tGyro.data(i,2)', res_tGyro.data(i,3)',res_tMagn.data(i,1)', res_tMagn.data(i,2)', res_tMagn.data(i,3)',res_tOr.data(i,1)', res_tOr.data(i,2)', res_tOr.data(i,3)',res_tRotQuatr.data(i,1)', res_tRotQuatr.data(i,2)', res_tRotQuatr.data(i,3)');
fprintf(fid, '\n');
end
fclose (fid);
}
here is the code. and here is the error:
Index exceeds matrix dimensions.
Error in resample2 (line 44) fprintf(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f', res_tAcc.data(i,1)', res_tAcc.data(i,2)', res_tAcc.data(i,3)',res_tGyro.data(i,1)', res_tGyro.data(i,2)', res_tGyro.data(i,3)',res_tMa
Sorry :/
Daniel Barzegar
Daniel Barzegar 2014년 6월 19일
i looked at the Workspace window and it seems that only the tAcc has values in it. The other 4 are empty (the same happens for the res_t*.data). any ideas why is that happening?
dpb
dpb 2014년 6월 19일
The other files don't exist or are somewhere else, maybe? Hard to say from here, you'll have to poke around at the command line and see what's what...

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

 채택된 답변

dpb
dpb 2014년 6월 19일
편집: dpb 2014년 6월 19일

2 개 추천

for i=1:size(res_tAcc.data,1)
fprintf(fid, '%f ', res_tAcc.time(i,1));
fprintf(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f', ...
res_tAcc.data(i,1)', res_tAcc.data(i,2)', res_tAcc.data(i,3)', ...
res_tGyro.data(i,1)', res_tGyro.data(i,2)', ...
..
Index exceeds matrix dimensions.
Error in resample2 (line 44) fprintf(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f', res_tAcc.data(i,1)', ...
You're running the loop over size(res_tAcc.data,1) but there's no guarantee that the others are necessarily identically the same size -- it appears at least one isn't as large.
Use size() on all the arrays you're wanting to output and ensure they're all at least as long as the one you've use.
BTW, for simplicity, a couple of things -- use repmat to make format strings manageable--
fmt=repmat('%f',1,15);
fprintf(fid,fmt,...
You can find the repeat count programmatically as well to save manual counting.
Also, build the array and output in one pass...
fmt=repmat('%f',1,16);
fprintf(fid,fmt,[res_tAcc.time(:,1) res_tAcc.data(:,1:3) res_tGyro.data(:,1:3) ...
res_tMagn.data(:,1:3) res_tOr.data(:,1:3) res_tRotQuatr.data(::3)].');
It would be even simpler and more regularly formatted if used dlmwrite or similar.
ERRATUM:
Also, build the array and output in one pass...
fmt=[repmat('%f',1,16) '\n'];
Forgot to add the newline...

추가 답변 (1개)

José-Luis
José-Luis 2014년 6월 19일
편집: José-Luis 2014년 6월 19일

2 개 추천

your_data = rand(60000,16);
fid = fopen('data.txt','w');
fprintf(fid, [repmat('%f ',1,size(your_data,2)) '\n'], your_data)
fclose(fid)
About 8MB. Please provide feedback on the answers you get.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2014년 6월 19일

편집:

dpb
2014년 6월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by