assign header line value to variable

조회 수: 3 (최근 30일)
Charles Gauthier
Charles Gauthier 2019년 7월 30일
편집: Adam Danz 2019년 7월 31일
I have a .vff file wich I open :
fid = fopen('test.vff');
The header is as follows:
ncaa;
rank=3;
type=raster;
format=slice;
bits=32;
bands=1;
size=256 256 256;
I want to extract size as a variable. So far I've tried:
size = fgetl(fid)
7 times in a row to get to the correct line, but the output I get is a char variable:
size =
'size=256 256 256;'
Instead of an array.
Thanks
CG
  댓글 수: 1
Stephen23
Stephen23 2019년 7월 31일
편집: Stephen23 2019년 7월 31일
Do NOT use size as a variable name, as doing so shadows the very important size function!
Shadowing inbuilt functions is a common cause of bugs.

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

채택된 답변

Adam Danz
Adam Danz 2019년 7월 30일
편집: Adam Danz 2019년 7월 31일
Once you have that string, convert it to a vector like this.
s = 'size=256 256 256;';
s(~isstrprop(s,'digit')) = ' ';
d = str2num(s)
% result
% d =
% 256 256 256
Option 2 (update after Stephen's good point below).
s = 'size=256 256 256;';
d = str2double(regexp(s,'\d+','match'))
Note that this will work even if the number of dimensions in 'size' changes (size = 256 256).
  댓글 수: 2
Charles Gauthier
Charles Gauthier 2019년 7월 30일
It worked! Thanks
Stephen23
Stephen23 2019년 7월 31일
편집: Stephen23 2019년 7월 31일
Note that str2num relies on eval.

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

추가 답변 (1개)

Stephen23
Stephen23 2019년 7월 31일
편집: Stephen23 2019년 7월 31일
A very simple and very efficient method without eval and without shadowing the inbuilt size:
>> str = 'size=256 256 256;';
>> vec = sscanf(str,'size=%f%f%f')
vec =
256
256
256

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by