フィルターのクリア

what is wrong with my code?

4 ビュー (過去 30 日間)
yanjie qi
yanjie qi 2016 年 2 月 28 日
コメント済み: Walter Roberson 2018 年 1 月 1 日
function [lxw,XO,wl]= HSIFileOpen(lxwfilepath,HSIfilepath)
%HSIFileOpen函数用来打开高光谱影像数据
fp1 = fopen(lxwfilepath,'r');
fp2 = fopen(HSIfilepath,'r');
lxw=fread(fp1,'%f');
bands = lxw(1);%波段数
datatype = lxw(2);%字节数
samples = lxw(3);%列数
lines = lxw(4);%行数
columns = samples*lines;%像元个数
switch lxw(5)%数据格式
case 0
interleave = 'bsq';
case 1
interleave = 'bil';
case 2
interleave = 'bip';
case 3
interleave = 'mat';
end
%读取高光谱数据二进制文件
switch datatype
case 1
precision = 'uint8';
case 2
precision = 'uint16';
case 4
precision = 'float32';
end
if interleave == 'bsq'
XO = fread(fp2,[columns,bands],precision);
XO = XO';
elseif interleave == 'bil'
tmp = fread(fp2,[samples,bands*lines],precision);
XO = zeros(bands,columns);
for i=1:bands
for j=1:lines
XO(i,((j-1)*samples+1):j*samples) = tmp(:,(j-1)*bands+i);
end
end
elseif interleave == 'bip'
XO = fread(fp2,[bands,columns],precision);
elseif interleave == 'mat'
XO = fread(fp2,[bands,columns],precision);
end
b = lxw(6);%该参数判断有没有波长数据
if b==1
token = strtok(HSIfilepath,'.');
wavelengthpath = strcat(token,'.wl');
fp3 = fopen(wavelengthpath,'r');
wl=fscanf(fp3,'%f');
%关闭文件
fclose(fp3);
else
wl = 0;
end
fclose(fp1);
fclose(fp2);
[EDITED, Jan, copied from tags:]
but the error is
input argument "lxwfilepath" is undefined.
error in ==> hsifileopen at 6 fp1 = fopen(lxwfilepath">
  1 件のコメント
Jan
Jan 2016 年 2 月 28 日
Please post the complete error message in your question, not a partial copy in the tags. Thanks.

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

採用された回答

Jan
Jan 2016 年 2 月 28 日
編集済み: Jan 2016 年 2 月 28 日
Guessing that the line 6 is this:
fp1 = fopen(lxwfilepath,'r');
I assume, that you call your function without defining input arguments. It has to be called like this:
[lxw, XO, wl] = HSIFileOpen(lxwfilepath, HSIfilepath)
Note: This will work in your case, because interleave has 3 characters in all cases:
if interleave == 'bsq'
But prefer strcmp for the comparison of strings:
if strcmp(interleave, 'bsq')
  3 件のコメント
Jan
Jan 2016 年 2 月 28 日
Sorry? What does "This?" mean?
yanjie qi
yanjie qi 2016 年 3 月 1 日
sir,
This, I mean the following code. Should I change this code? There is something wrong with it. I change it as you suggest

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

その他の回答 (3 件)

yanjie qi
yanjie qi 2016 年 2 月 28 日
編集済み: Walter Roberson 2016 年 3 月 1 日
I run the M file,and the answer is this:
>> [lxw,XO,wl]= HSIFileOpen('F:\多光谱材料\pca\2.hdr','F:\多光谱材料\pca\2.raw')
??? Error using ==> fread
Invalid precision.
Error in ==> HSIFileOpen at 10
lxw=fread(fp1,'%f');
  3 件のコメント
yanjie qi
yanjie qi 2016 年 3 月 1 日
編集済み: Walter Roberson 2016 年 3 月 1 日
SIR,thanks.
I wrote as you suggest, however here is another problem,
??? Undefined function or variable "interleave".
Error in ==> HSIFileOpen at 35
if strfind(interleave, 'bsq')
Is there anything wrong with strfind? or I will try strcmp? but it still hsa error:
[lxw,XO,wl]= HSIFileOpen('F:\多光谱材料\pca\2.hdr','F:\多光谱材料\pca\2.raw')
??? Undefined function or variable "interleave".
Error in ==> HSIFileOpen at 35
if strcmp(interleave, 'bsq')
WHAT SHOULD I DO? THANK YOU
Walter Roberson
Walter Roberson 2016 年 3 月 1 日
That code does not protect against the possibility of unexpected content in the data file. That code only defines the variable named interleave if a particular location in the file contains 0, 1, 2, or 3. At the very least the code should have an "otherwise" on the switch statement that generates an error saying that the file is not in the expected format.

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


Walter Roberson
Walter Roberson 2016 年 3 月 1 日
You should be considering using http://www.mathworks.com/matlabcentral/fileexchange/29344-read-medical-data-3d from the File Exchange, as it reads .raw files with .hdr . It is tested code.
  2 件のコメント
yanjie qi
yanjie qi 2016 年 3 月 1 日
thank you. sir, I will try.
yanjie qi
yanjie qi 2016 年 3 月 1 日
SIR,
??? Error using ==> analyze75info>parseInputs at 457 F:\多光谱材料\pca\2.hdr is not a valid Analyze 7.5 format file.
It looks like something wrong with my .hdr file?

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


Mehdi Mosafer
Mehdi Mosafer 2018 年 1 月 1 日
I think this issue is because of using non-ASCII alphabets for naming a folder in which the data file is located.
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 1 月 1 日
No, the user did not pass the file name in to the function.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by