From: jason on
hello,

i am running the code below and when i inspect the txt file later on
it is indecipherable and is not able to be read back in when i use
fread.

the data in the matrix is of class "double", and i am attempting to
also write to double.

not sure what is causing this.
thank you!!

see code below for two tries i gave neither of which worked
:::::
[fid,message]=fopen(strcat('/Users/Jason/Desktop/
folder/',Tick_Hold,'/',TS(1,j).filename{1,i},'.txt'),'a');

fwrite(fid,TS(1,j).ts(i,:),'double');

fclose(fid);

[fid,message]=fopen(strcat('/Users/Jason/Desktop/
folder/',Tick_Hold,'/',TS(1,j).filename{1,i},'.txt'),'a');
for q=1:size(TS(1,j).ts(i,:),2)
fwrite(fid,TS(1,j).ts(i,q),'double');
end
fclose(fid);

From: Walter Roberson on
jason wrote:

> i am running the code below and when i inspect the txt file later on
> it is indecipherable and is not able to be read back in when i use
> fread.

How exactly do you "inspect" the txt file later? When you use fwrite,
you are writing in binary, and should not expect to be able to make
anything out of inspecting the file as if it were text. If you want to
write out doubles to be human-readable text, use fprintf().

How exactly are you trying to read the file back in with fread() ?

> fwrite(fid,TS(1,j).ts(i,:),'double');

That should work, producing a binary file.
From: jason on
On Mar 10, 12:12 am, Walter Roberson <rober...(a)hushmail.com> wrote:
> jason wrote:
> > i am running the code below and when i inspect the txt file later on
> > it is indecipherable and is not able to be read back in when i use
> > fread.
>
> How exactly do you "inspect" the txt file later? When you use fwrite,
> you are writing in binary, and should not expect to be able to make
> anything out of inspecting the file as if it were text. If you want to
> write out doubles to be human-readable text, use fprintf().
>
> How exactly are you trying to read the file back in with fread() ?
>
> >         fwrite(fid,TS(1,j).ts(i,:),'double');
>
> That should work, producing a binary file.

hi walter,

a=fread(fid,100,'double')

this is not working but i cannot see an error.
From: Walter Roberson on
jason wrote:
> On Mar 10, 12:12 am, Walter Roberson <rober...(a)hushmail.com> wrote:
>> jason wrote:
>>> i am running the code below and when i inspect the txt file later on
>>> it is indecipherable and is not able to be read back in when i use
>>> fread.
>> How exactly do you "inspect" the txt file later? When you use fwrite,
>> you are writing in binary, and should not expect to be able to make
>> anything out of inspecting the file as if it were text. If you want to
>> write out doubles to be human-readable text, use fprintf().
>>
>> How exactly are you trying to read the file back in with fread() ?
>>
>>> fwrite(fid,TS(1,j).ts(i,:),'double');
>> That should work, producing a binary file.
>
> hi walter,
>
> a=fread(fid,100,'double')
>
> this is not working but i cannot see an error.

You open the file in append mode. If there is already something in the file,
then the data you write will go after the existing content, and when you go to
fread() the file, you will need to skip that existing content.

Also, to confirm: you are closing and reopening the file before trying to do
the read? When you open in 'a' mode, seeking back to the beginning of the
file and reading from there is theoretically not allowed.



>> TS.ts(1,:) = 1./(1:100)
TS =
ts: [1x100 double]
>> TS.ts
ans =
Columns 1 through 3
1 0.5 0.333333333333333
Columns 4 through 6
0.25 0.2 0.166666666666667
<etc>

>> j = 1; i = 1;
>> fid = fopen('/tmp/jason.dat','a')
fid =
3
>> fwrite(fid,TS(1,j).ts(i,:),'double')
ans =
100
>> fclose(fid)
ans =
0
>> fopen('/tmp/jason.dat','r')
ans =
3
>> fread(fid,100,'double')
ans =
1
0.5
0.333333333333333
0.25
0.2
0.166666666666667
<etc>