This snippit works in base program, but not in a function.

Have tried strjoin and several other commads with no success. Help?
opathName=[opath,'\',iname, num2str(a),'.tif'];
imwrite(uint16(I (:,:,a)),opath_ opathName,'tif','Compression','none'); tiff

7 件のコメント

Adam
Adam 2015 年 11 月 16 日
I assume that when you use a function you have passed in or created in its workspace all the variables it needs for that instruction?
That is the most obvious reason why something would work on command line, but not in a function - functions have their own sealed workspace with nothing in it that isn't either passed in or created (or global parameters, but even these still have to be declared in the function to be used)
Walter Roberson
Walter Roberson 2015 年 11 月 16 日
What error message do you observe?
SteveH
SteveH 2015 年 11 月 16 日
Adam: Yes I've passed or created the variables [ I have made that mistake in past so reasonable question]
Walter: When I print opathname fm the function I get extra spaces in name and this error 'Error using imwrite (line 455) Unable to open file "C:\path\ iname 1 .tif" for writing. You may not have write permission.' . If I add delimiters [applies to any delimiter] to strjoin I get this error 'Unexpected MATLAB operator'.
SteveH
SteveH 2015 年 11 月 17 日
編集済み: Image Analyst 2015 年 11 月 17 日
FYI for anybody interested found solution: have to use 'char' in function but not base program
opath_name = char(strcat(opath,'\',iname, num2str(a),'.tif'));
imwrite(uint16(I (:,:,a)),opathName,'tif','Compression','none');
Walter Roberson
Walter Roberson 2015 年 11 月 17 日
You should get in the practice of using fullfile() and sprintf()
My guess is that either your opath or your iname is a cell array of strings when you have the problem. You did not show how you constructed them so we cannot point out the reason as yet.
Image Analyst
Image Analyst 2015 年 11 月 17 日
That still won't work. What a mess - I'd much rather maintain Walter's code than yours, and I suggest you take his suggestion. By the way, to fix, change opathName to opath_name.
SteveH
SteveH 2015 年 11 月 17 日
thanks Walter. I'll use fullfile(). learn something new every day.

回答 (0 件)

この質問は閉じられています。

質問済み:

2015 年 11 月 16 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by