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 件のコメント

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

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 17 日

0 投票

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 件のコメント

Yann Yiing
Yann Yiing 2017 年 11 月 17 日
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.
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
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, ....)
Yann Yiing
Yann Yiing 2017 年 11 月 17 日
i see.
Yann Yiing
Yann Yiing 2017 年 11 月 17 日
Then do i need to reopen the file again?
Yann Yiing
Yann Yiing 2017 年 11 月 17 日
I think i solved it by reopening the file again but im not sure yet.
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
Reopen might work, or frewind(fid) instead of reopen.
Yann Yiing
Yann Yiing 2017 年 11 月 17 日
Okay i will give it a try.
Yann Yiing
Yann Yiing 2017 年 11 月 17 日
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.
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
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 2017 年 11 月 17 日
編集済み: Yann Yiing 2017 年 11 月 17 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2017 年 11 月 17 日

編集済み:

2017 年 11 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by