Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
about dlmwrite

Subject: about dlmwrite

From: dhuan Du

Date: 3 Jul, 2009 14:27:01

Message: 1 of 5

Hi, friends:
I need to use dlmwrite to write Matrix into text fiel in a loop. The following is my codes and error information. Could anyone give me some help?

My codes:
 
for s=1:10
        
        Wfname=strcat('WfS',int2str(s),'.txt');
        fopen(Wfname,'a');
        dlmwrite((Wfname, M,'-append', 'roffset', 1,'delimiter','\t','precision',10,'newline', 'pc'); %M is a matrix

end


??? Error using ==> dlmwrite at 126
Could not open file WfS2.txt

Error in ==> main_630 at 151
        dlmwrite(Wfname, M, '-append','roffset', 1, 'delimiter','\t','precision',10,'newline',
        'pc');

Subject: about dlmwrite

From: Image Analyst

Date: 3 Jul, 2009 14:57:01

Message: 2 of 5

Well after you opened it for writing with fopen, it probably can't be opened again for writing. fopen() probably locked it so that when dlmwrite comes along and tries to open it, it can't. Why did you put that fopen() in there????? What is the purpose of it? Remove it and it should work.

Subject: about dlmwrite

From: dhuan Du

Date: 6 Jul, 2009 13:33:01

Message: 3 of 5

Hi,

Thanks for your suggestion. I put fopen() in front of dlmwrite becasue I would like to artifically open the text file, export data to it (dlmwrite), and then close it in a loop. Previously, I did not use fopen(), but sometimes I still got the error said that the text file is locked and dlmwrite cannot open it (I guess it is locked by the previous iteration), when dlmwrite try to export data in this iteration.

Do you have any idea how to repeat export matrix to a text file in a loop?

Thanks again.

Dhuan

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <h2l67s$ehn$1@fred.mathworks.com>...
> Well after you opened it for writing with fopen, it probably can't be opened again for writing. fopen() probably locked it so that when dlmwrite comes along and tries to open it, it can't. Why did you put that fopen() in there????? What is the purpose of it? Remove it and it should work.

Subject: about dlmwrite

From: dpb

Date: 6 Jul, 2009 14:44:48

Message: 4 of 5

dhuan Du wrote:
...

[PLEASE don't toppost...it makes following a conversation difficult]

> Thanks for your suggestion. I put fopen() in front of dlmwrite
> becasue I would like to artifically open the text file, export data
> to it (dlmwrite), and then close it in a loop. Previously, I did not
> use fopen(), but sometimes I still got the error said that the text
> file is locked and dlmwrite cannot open it (I guess it is locked by
> the previous iteration), when dlmwrite try to export data in this
> iteration.

That should _NOT_ occur...if you can make a definitive test case that
indicates a locked file problem w/o an extraneous fopen or other access
of a given file, contact TMW tech support.

> Do you have any idea how to repeat export matrix to a text file in a
> loop?

What do you _really_ want here?

In your sample code

for s=1:10
   Wfname=strcat('WfS',int2str(s),'.txt');
   dlmwrite((Wfname, M,'-append', 'roffset', 1, ...
             'delimiter','\t','precision',10,'newline', 'pc');

end

the file name is being changed every loop so unless there's an existing
file of that name already there's no need (or use) for the "-append"
operation is a starter.

The row offset value of "1" implies maybe this (hopefully existing) file
has a header line already? If so, perhaps there might be a missing
newline in the existing file that's not helping altho don't see why that
would cause a locked file.

It sounds more to me like the issue could be there's another application
at play here creating these files and that app may have the file locked?

Again, a description of what, precisely, you're trying to accomplish
rather than just the error this approach is giving might be more useful.

--

Subject: about dlmwrite

From: dpb

Date: 6 Jul, 2009 17:51:12

Message: 5 of 5

dpb wrote:
...
> The row offset value of "1" implies maybe this (hopefully existing) file
> has a header line already? ...

Also, I've no clue what would happen in dlmwrite() w/ the combination of
"-append" and "'roffset', 1" if there happens to be more than a single
line in the file and I can't test it since my ML version predates the
additional functionality.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us