why does size() not work in function but does in workspace

7 ビュー (過去 30 日間)
SteveH
SteveH 2015 年 12 月 1 日
コメント済み: SteveH 2015 年 12 月 1 日
I have a the same 4x2 cell array (fileList), in workspace, size() gives me the results I expect, but in a function results are way different.
Workspace
size(fileList), ans=4 2
size(fileList{2,1},1), ans = 9
function
size(fileList), ans=1 1
size(fileList{2,1},1), ans = Index exceeds matrix dimensions.
Other functions that don't behave as expected when not in workspace:
fileList{n,m}
fileList(n,:)
  2 件のコメント
Guillaume
Guillaume 2015 年 12 月 1 日
Can we see the declaration of the function (the line that says function something = funname(something)) and the way you call the function?
SteveH
SteveH 2015 年 12 月 1 日
編集済み: Guillaume 2015 年 12 月 1 日
workspace
fileList= <4x2 cell>
readvars ={filelist,xdim,ydim,bitdepth,skip_bytes,pad_bytes,cntr};
imageList=readRaw (readvars);
function imageList = ReadRaw (readVars)
tfileList = readVars(1); .........
the function recognizes tfileList as a 4x2 cell

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

採用された回答

Guillaume
Guillaume 2015 年 12 月 1 日
You need to understand the difference between () and {} when applied to cell array. () returns a portion of a cell array as a cell array. {} returns the content of the cell array.
So, readvars(1) is a 1x1 cell array which is just the readvars cell array trimmed to 1 element
readvars{1} is the first element of the cell array.
Therefore, change the first line of your function to
tfilelist = readVars{1};
and all shall be well.
  1 件のコメント
SteveH
SteveH 2015 年 12 月 1 日
Thanks to valdal: I could see passing cell array like in his example worked, so it had to be..... Thanks to Guillaume: I trip over those darn ({[]}) too often.
ALL IS WELL!

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

その他の回答 (1 件)

valdal
valdal 2015 年 12 月 1 日
Hi,
On my computer, I don't have any problem :
fileList = cell(4,2)
fileList{2,1} = rand(9,1)
size(fileList)
size(fileList{2,1},1)
f(fileList)
with f.m :
function f(a)
size(a)
size(a{2,1},1)
end
In both cases I got :
ans =
4 2
ans =
9
Are you sure that you give the whole cell array to your function ? It's look like in the function fileList is only one element.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by