Re: [討論] function的intput如何為字串&字元

作者: celestialgod (天)   2015-10-20 01:07:53
※ 引述《Souseasou3 (Almighty)》之銘言:
: 如題
: 最近遇到幾個問題,讓我迫切想知道如何讓function的intput為字串字元
: ex:
: 1.)
: 此function為輸入欲知的羅馬數字符號,然後output為所對應的數字
: 如 input=I output=1 ; intput=II output=2 ......
: 最接近的作法為intput函數,但我想知道有辦法直接在這個function的intput直接輸入字
: 串&字元?
看不懂你在寫什麼...不是本來就可以輸入字串了嗎= =
function out = feedback(in)
out = in;
end
feedback('in1') % 'in1'
你如果說的是這種:
function out_opts = structMapping(varargin)
option_default = struct('in1', 5, 'in2', 10);
optsCell = [fieldnames(option_default), struct2cell(option_default)];
optionsReplace = reshape(varargin, 2, [])';
[loc2,loc1] = ismember(optionsReplace(:,1), fieldnames(option_default));
optsCell(loc1(loc1~=0), 2) = optionsReplace(loc2, 2);
out_opts = cell2struct(optsCell(:, 2), optsCell(:,1));
end
structMapping('in1', 7)
output:
1 x 1 struct:
in1: 7
in2: 10
structMapping('in1', 7, 'in2', -7)
output:
1 x 1 struct:
in1: 7
in2: -7
還是這種
function out = returnInputName(in)
out = inputname(1);
end
SSS = 1;
returnInputName(SSS) % 'SSS'
: 2.)
: 如何寫一個function,其intput為輸入圓的 中心 半徑 顏色
: 然後此function就會依照對應數據plot出這個圓
: 但如何在在圓的intput輸入字元(顏色)呢?
: p.s. r 為紅色 b 為藍色 ......
: 麻煩高手求解?
function [] = plotCircle(origin, r, col)
theta = linspace(0, 2*pi);
plot(r*cos(theta) + origin(1), r*sin(theta) + origin(2), 'Color', col);
end
plotCircle([0, 0], 5, 'r')
Or you can use anonymous function
plotCircle = @(x0, y0, r, col) plot(r*cos(linspace(0,2*pi)) + x0, ...
r*sin(linspace(0,2*pi)) + y0, 'Color', col);
plotCircle(0, 0, 5, 'r')
補充一點 畫圓記得設定 axis square 以保證兩軸等長看起來是圓

Links booklink

Contact Us: admin [ a t ] ucptt.com