Hello
I'm wondering if anyone can help me. I don't have any experience creating binary files or doing what I need to do so this might be a silly question :)
I have a 3D matrix, 24x10x24 (time X layers X nodes). I'm trying to write a for loop for that it runs through the 10 layers with both time and nodes being 1, then I need it to run through each each layer with time being 1 and nodes being 2, and so on for each node. e.g
% [Double] U(1,1,1)
% [Double] V(1,1,1)
% ...[Double] U(nLayers,1,1)
% [Double] V(nLayers,1,1)
% [Double] U(1,2,1)
% [Double] V(1,2,1)
% ...[Double] U(nLayers,2,1)
% [Double] V(nLayers,2,1)
% ... ... ...[Double] U(nLayers,nNodes,1)
% [Double] V(nLayers,nNodes,1)
% [Double] U(1,1,2)
% [Double] V(1,1,2)
% ...[Double] U(nLayers,1,2)
% [Double] V(nLayers,1,2)
% [Double] U(1,2,2)
% [Double] V(1,2,2)
% ...[Double] U(nLayers,2,2)
% [Double] V(nLayers,2,2)
% ... ... ...[Double] U(nLayers,nNodes,2)
% [Double] V(nLayers,nNodes,2)
I've written some code, but I don't think its right at all since I can't really wrap my head around what I need to do anyway. And I probably haven't explained myself very well. If anyone has any ideas, I would really welcome them :)
Code so far (very wrong):
for lp=1:length(nLayers);
fwrite(fileID,FV.u(1,lp,1),'double','ieee-be');
fwrite(fileID,FV.v(1,lp,1),'double','ieee-be');
for lq=1:length(nNodes);
fwrite(fileID,FV.u(1,lp,lq),'double','ieee-be');
fwrite(fileID,FV.v(1,lp,lq),'double','ieee-be');
for lt=1:length(nTimeStep);
fwrite(fileID,FV.u(lt,lp,lq),'double','ieee-be');
fwrite(fileID,FV.v(lt,lt,lp),'double','ieee-be');
end
end

 채택된 답변

Walter Roberson
Walter Roberson 2016년 10월 25일

0 개 추천

Your code writes u(1,lp,1) and then writes it again as the first element of u(1,lp,lq)

댓글 수: 4

Meghan
Meghan 2016년 10월 25일
I know, that's why I've said my code is very wrong.
I need it to run through each element in lp for every element in lq. I just don't think my code does that. And because I'm creating a binary file, I can't check to see if it actually works properly.
Walter Roberson
Walter Roberson 2016년 10월 25일
Use permute() so that the data is ordered with the data to be written first runs along the first column, then the second, to end of first layer, then along first column of next layer and so on. Now reshape to a row vector. Do the same for v including reshape to a row vector. vertcat the two row vectors u above v. Now fwrite the result all as a single call.
Walter Roberson
Walter Roberson 2016년 10월 25일
If you are using Linux or OS X then you can use "od" to examine the contents of the binary file
Meghan
Meghan 2016년 10월 25일
Sorry I realised that I'd already written the code and got the answer I wanted. I just had it a bit fuzzy. Never written anything to binary before so a little all over the place!
Thanks for the help anyway :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2016년 10월 25일

댓글:

2016년 10월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by