When writing data in "fwrite", can I write data in the row direction?

7 views (last 30 days)
D = 'data.raw'
data_fid = fopen(D)
for i = 1:100
for j = 1:10
data_frd{1,j} = fread(data_fid,[row, column],'uint16')
end
end
data_frd_m = [data_frd{1,1};data_frd{1,2};data_frd{1,3};data_frd{1,4};data_frd{1,5};data_frd{1,6};data_frd{1,7};data_frd{1,8};data_frd{1,9};data_frd{1,10},]
fwrite(data_fid,data_frd_m,'uint16')
There is no process of combining the above code like the 'data_frd_m' variable.
Is there a way to write data immediately by putting "fwrite" in the for loop and then writing it down to the line below?
for i = 1:100
for j = 1:10
data_frd{1,j} = fread(data_fid,[row, column],'uint16')
fwrite(data_fid,data_frd_m,'uint16') <<-
end
end
Alternatively,
data_frd_m = [data_frd{1,1};data_frd{1,2};data_frd{1,3};data_frd{1,4};data_frd{1,5};
data_frd{1,6};data_frd{1,7};data_frd{1,8};data_frd{9};data_frd{10}]
What is a simple way to write this code?
  1 Comment
Walter Roberson
Walter Roberson on 21 Mar 2022
Note that you can abbreviate
data_frd_m = [data_frd{1,1};data_frd{1,2};data_frd{1,3};data_frd{1,4};data_frd{1,5};data_frd{1,6};data_frd{1,7};data_frd{1,8};data_frd{1,9};data_frd{1,10},]
to
data_frd_m = vertcat(data_frd{1,1:10});

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 21 Mar 2022
Is there a way to write data immediately by putting "fwrite" in the for loop and then writing it down to the line below?
Yes, but it is unlikely to be what you want to do.
First, do any writing you want to do up to that point, then ftell() to figure out where you are in the file.
Next, find out the number of rows of each cell entry of interest.
Start a loop over the columns of the first item. Write one column for the first variable. Now write as many zeros as there are rows in all remaining variables. Write the next column for the first variable, then as many zeros as there are rows in all remaining variables. Keep doing that until you have written the last column for the first variable and the zeros after that. At the end of all of this, you will have written as much information as the total size needed for the variables, but only the rows corresponding to the first variable will be meaningfull, with the rest being zeros for padding.
Now loop over the variables. fseek() back to the point of the initial ftell(). fseek() further on by as much storage as needed to hold one column for all previous variables. fwrite() one column. If there are further columns, fseek() by as much storage as needed to hold one column for each of the following variables, plus one row for each of the previous variables. Now you can fwrite another column. Keep repeating this work of in-filling meaningful data into the file, one column at a time for each variable.
This is not efficient. You should not do this unless you Have A Very Good Reason.
Might I suggest to you that all you really need to do is the vertcat() I posted in the comment above?
(If the problem is that you cannot afford the storage to do the vertcat() then you should investigate whether a different storage organization would be permitted.

Community Treasure Hunt

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

Start Hunting!