The code only works on Evaluation

This code only when run normally, N_id, N_x, N_y and N_z will give me nothing. But when i used Evaluate on the codes. It actually work, but i am not sure why
FileName = ['C:\Users\ying0018\Desktop\Right Femur\Surface_Baseline_R.inp']
disp('*******************************************************************************************************')
disp(['Current Directory: ', FileName]);
disp('*******************************************************************************************************')
fid = fopen(FileName);
SWData = textscan (fid, '%s', 'delimiter', '\n', 'whitespace', '');
SWStr = SWData{1};
NBD = strfind(SWStr,'*NODE');
Node_begin = find(~cellfun('isempty', NBD));
NED = strfind(SWStr, '**HWCOLOR COMP ');
Node_end = find(~cellfun('isempty', NED));
Node_range = (Node_end - (Node_begin + 1));
% ND : Node data, N: Nodess
ND = textscan(fid,'%d %f %f %f', Node_range,'delimiter',',','headerlines', Node_begin, 'CommentStyle','**');
N_Id = ND{1,1};
N_x = ND{1,2};
N_y = ND{1,3};
N_z = ND{1,4};
Nodes = [N_Id, N_x, N_y, N_z];

6 Comments

Difficult to say without a copy of your input file.
Also, what does it mean to use Evaluate in this context?
Can't attach the inp file.
My evaluate i mean that you highlight the codes and then right click. Then click on Evaluate Selection and the code works.
You can zip the inp file and attach the zip
What name did you give to the file that the code is stored in?
I have attached the inp file. The file containing codes for it is known as Format_Surface.m

Sign in to comment.

 Accepted Answer

You have
ND = textscan(fid,'%d %f %f %f', Node_range,'delimiter',',','headerlines', Node_begin, 'CommentStyle','**');
the %d tells textscan to create an int32 data type; the %f create double data type. This conflict in data type is not a problem at that point because the output is a cell array with one cell per column, and each column is going to be consistent as to its data type.
You then extract the columns
N_Id = ND{1,1};
N_x = ND{1,2};
N_y = ND{1,3};
N_z = ND{1,4};
The N_Id is going to be int32 because that is what it was from the %d format. The other three variables are going to be double.
You then
Nodes = [N_Id, N_x, N_y, N_z];
This tells it to do horizontal concatenation of the four arrays, the first of which is int32 and the others of which are double. When you concatenate arrays of different data types, the output is always the most restrictive of the data types, so this would be equivalent to
Nodes = [N_Id, int32(N_x), int32(N_y), int32(N_z)]
which is not likely what you want.
The fix is to either use %f for the first column or else to use
Nodes = [double(N_Id), N_x, N_y, N_z];

10 Comments

N_Id, N_x, N_y and N_z are all blank. Somehow they can't get the data from ND. Because there 4 are blank. Naturally Nodes will be blank too. The problem lies with them not capturing the data.
You have
SWData = textscan (fid, '%s', 'delimiter', '\n', 'whitespace', '');
That is going to read the entire file, leaving it at end of file at the time of your second textscan(fid, ....)
Then do i need to reopen the file again?
I think i solved it by reopening the file again but im not sure yet.
Reopen might work, or frewind(fid) instead of reopen.
Okay i will give it a try.
Yep correct. If you rewrite your answer by saying because it is at the end of the file. and using the frewind function can solve the problem. Then I can accept your answer. Cos currently, the original answer does not solve the issue.
You can accept anyhow, because one of the follow-ups I gave in the thread was the solution to that part. It is more common than not that the initial answer does not provide the complete solution.
But you also need to fix the int32 problem.
Yann Yiing
Yann Yiing on 17 Nov 2017
Edited: Yann Yiing on 17 Nov 2017
yep i fixed by using %f instead of %d. Thanks for helping me out. Been searching the MATLAB community for the answers for 3 days before asking it.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!