plot with input arguments
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
i want to subplot 4 string containg cell array from ( sin, cos, tan,sinh) and vector (+,o,*,x,s) and the mathmatical function should be ploted in interval (-2pi,2pi)with step 0.3
採用された回答
Walter Roberson
2019 年 12 月 5 日
funs_cell = {'sin', 'cos', 'tan', 'sinh'};
markers = {'+', 'o', '*', 'x', 's'};
funs = cellfun(@str2fun, funs_cell, 'uniform', 0);
for K = 1 : length(funs)
plot(x, funs{K}(x), markers{K}, 'DisplayName', funs_cell{K});
hold on
end
33 件のコメント
Roger Nadal
2019 年 12 月 5 日
If I pass more value in fun_cell then it should select only four value while passing function so how it should work?
Walter Roberson
2019 年 12 月 5 日
for K = 1 : 4
Or did you want it to select 4 at random?
Roger Nadal
2019 年 12 月 6 日
yes i want to select random four and it should plot 4 subplot in one figure like function
abc({sin', 'sinh', 'cos', 'cosh'}, ['o', 'x', 's', '*']) it should plot four in one figure.
Walter Roberson
2019 年 12 月 6 日
It would be better if the markers were passed as a cell array rather than as a character vector. With a cell array you would have the option of passing line styles and line colors as well.
function abc(funs_cell, markers)
funs = cellfun(@str2fun, funs_cell, 'uniform', 0);
order = randperm(length(funs), 4); %4 of them at random
for K = order
subplot(2,2, K)
plot(x, funs{K}(x), markers{K});
title(funs_cell{K});
end
end
Roger Nadal
2019 年 12 月 6 日
編集済み: Roger Nadal
2019 年 12 月 6 日
is it str2fun or str2func? and how to pass line colour using cell array?
Walter Roberson
2019 年 12 月 6 日
Ah, str2func.
I was sure I had tested before; I must have transcribed it wrong when I posted.
abc({sin', 'sinh', 'cos', 'cosh'}, {'r-o', 'b-x', 'g-s', 'k--*'})
Roger Nadal
2019 年 12 月 6 日
is there is any way to solve this without using any loop?and i want to pass markers argument and colours argument in vector with [ ] brace indexing
Walter Roberson
2019 年 12 月 6 日
You could use [] indexing if you pass "" delimited string objects.
If you restrict to character vectors then when you use ['o', 'x', 's', '*'] then MATLAB cannot tell that apart from the case where you passed 'oxs*' as a single character vector. You would lose the flexibility of having specifications of different length. You could handle the case where the user always passed exactly the same number of characters per specification. For example you would not be able to specify dashed line with '--' unless the user always passed two characters for the line type even when they only wanted '-' or ':' line type.
Roger Nadal
2019 年 12 月 6 日
編集済み: Roger Nadal
2019 年 12 月 6 日
And if I have to pass colour as third argument in function so should I just define it in function and plot it? abc({'sin','cos','tan','sinh'},['b','r','g','y'],['o','x','*','s']) so the first argument will be cell array and other two will be vector. and how to plot step 0.4 for each marker.
Walter Roberson
2019 年 12 月 7 日
I don't know why you insist on not using a loop. I don't know why you insist on using character vectors for your markers and colors instead of the much more flexible cell array. But I no longer have patience to lead you through this, so I will give you what you are asking for, even though it is not what you should be using.
function abc(funs_cell, colors, markers)
x = -2*pi:0.4:2*pi;
order = randperm(length(funs),4); %random order
fun = str2fun(funs_cell{order(1)});
subplot(2,2,1);
plot(x, fun(x), 'LineStyle', '-', 'Color', colors(1), 'Marker', markers(1));
title(funs_cell{order(1)});
fun = str2fun(funs_cell{order(2)});
subplot(2,2,2);
plot(x, fun(x), 'LineStyle', '-', 'Color', colors(2), 'Marker', markers(2));
title(funs_cell{order(2)});
fun = str2fun(funs_cell{order(3)});
subplot(2,2,3);
plot(x, fun(x), 'LineStyle', '-', 'Color', colors(3), 'Marker', markers(3));
title(funs_cell{order(3)});
fun = str2fun(funs_cell{order(4)});
subplot(2,2,4);
plot(x, fun(x), 'LineStyle', '-', 'Color', colors(4), 'Marker', markers(4));
title(funs_cell{order(4)});
end
Roger Nadal
2019 年 12 月 7 日
funs is not define variable so is it fun or funs in length(funs)?
Walter Roberson
2019 年 12 月 7 日
function abc(funs_cell, colors, markers)
x = -2*pi:0.4:2*pi;
order = randperm(length(funs_cell),4); %random order
fun = str2fun(funs_cell{order(1)});
subplot(2,2,1);
plot(x, fun(x), 'LineStyle', '-', 'Color', colors(1), 'Marker', markers(1));
title(funs_cell{order(1)});
fun = str2fun(funs_cell{order(2)});
subplot(2,2,2);
plot(x, fun(x), 'LineStyle', '-', 'Color', colors(2), 'Marker', markers(2));
title(funs_cell{order(2)});
fun = str2fun(funs_cell{order(3)});
subplot(2,2,3);
plot(x, fun(x), 'LineStyle', '-', 'Color', colors(3), 'Marker', markers(3));
title(funs_cell{order(3)});
fun = str2fun(funs_cell{order(4)});
subplot(2,2,4);
plot(x, fun(x), 'LineStyle', '-', 'Color', colors(4), 'Marker', markers(4));
title(funs_cell{order(4)});
end
Roger Nadal
2019 年 12 月 7 日
is there is any way to change points for x axes like in plot the points of x axes is [-5 5] instead i want [-10 10].
Walter Roberson
2019 年 12 月 7 日
xlim([-10 10])
However you should be considering changing the definition of x instead. Your requirements were that it be -2*π to 2*π. Did that requirement change?
Roger Nadal
2019 年 12 月 7 日
編集済み: Roger Nadal
2019 年 12 月 7 日
no the function should plot on interval [-2pi ,2pi] but the x axes i want [-10 10]. the order of plot is different than argument pass in fuction {sin', 'sinh', 'cos', 'cosh'} the plot order is sinh,cosh,sin,cos.
Walter Roberson
2019 年 12 月 7 日
xlim controls the axes boundaries no matter what is plotted inside.
You asked for random order.
Roger Nadal
2019 年 12 月 7 日
Ok got it.
Roger Nadal
2019 年 12 月 9 日
if i am passing more than 4 input then it should output an error input vector length is different than 4 so how to put this condition in the program?
Walter Roberson
2019 年 12 月 9 日
numels(variable) ~= 4
Roger Nadal
2019 年 12 月 9 日
It can be done using if conditions?
Walter Roberson
2019 年 12 月 9 日
if numels(funs_cell) ~= 4
error('Opps, you did it again!')
end
Roger Nadal
2019 年 12 月 10 日
It works for math function but if I am passing 5 values in market and colour it doesn’t work for that
Walter Roberson
2019 年 12 月 10 日
Did you add similar tests for those variables?
Roger Nadal
2019 年 12 月 10 日
It can’t be done in one condition?
Walter Roberson
2019 年 12 月 10 日
You can combine three tests in a single if using the || operator.
This is a way to code it as a single if, but you would not be expected to work it out at the level of your homework assignment.
Roger Nadal
2019 年 12 月 10 日
So I have to use 3 different if conditions?
Walter Roberson
2019 年 12 月 11 日
編集済み: Walter Roberson
2019 年 12 月 11 日
if A || B || C
error('Nada!')
end
Roger Nadal
2019 年 12 月 11 日
Using this if i am passing 4 values then it also showing error
Walter Roberson
2019 年 12 月 11 日
Please show your attempt.
Roger Nadal
2019 年 12 月 11 日
If numel(fun_cell)||numel(color)~=4 Error(msg)
Walter Roberson
2019 年 12 月 11 日
In MATLAB,
if numel(fun_cell)||numel(color)~=4 Error(msg)
would be interpreted as
if (numel(fun_cell) ~= 0) || (numel(color) ~= 4)
error(msg)
end
MATLAB itself has absolutely no transitive operators of the form
expression1 operator1 expression2 operator2 value
being intepreted as
(expression1 operator2 value) operator1 (expression2 operator2 value)
In MATLAB you need to be explicit,
if numel(fun_cell) ~= 4 || numel(color) ~= 4
error(msg)
end
Roger Nadal
2019 年 12 月 11 日
Thank you
Roger Nadal
2019 年 12 月 11 日
If I don’t want to generate random order for then should I remove randperm?
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
