app designerで、​スライダーとplot​の連動のさせ方が分か​りません

11 views (last 30 days)
App desingerにて、需要・供給曲線をplotするアプリを作成しております。
(1)スタートボタンを押したら、コールバックして、データをプロットする
(2)"y軸の価格"と"スライダの目盛"を連動させて、スライダーを動かしたら、需要曲線(青い方)の上にマークが出るようにする
(3)スライダーを動かすたびに、供給曲線のxの値と需要曲線のxの値を取得して、計算結果をスライダーの下に表示させる
という順番でアプリを作成しようと考えており、(1)の部分は出来たのですが、(2)で躓いております。スライダーのコールバックで、ValueChangedFunでコールバックはしてみたのですが、コールバックした際に、どうやったらスライダの目盛と需要曲線を連動させることが出来るでしょうか?Chat GPTに聞きながら試行錯誤をしているのですがうまく実装出来ず、何かアドバイスを頂けると大変ありがたいです。
(1)のコード
% Callback function: Button_5, Tab, UIAxes
function demandSupply(app, event)
% 価格、需要、供給のデータをテーブルとして定義
data = table([10000; 30000; 50000; 70000; 90000; 110000; 130000; 150000], ...
[4000; 3500; 3000; 2500; 2000; 1500; 1000; 500], ...
[700; 1400; 2100; 2800; 3500; 4200; 4900; 5600], ...
'VariableNames', {'Price', 'Demand', 'Supply'});
% プロット
blue = plot(app.UIAxes, data.Demand, data.Price, 'b-', 'LineWidth', 2, 'DisplayName', '需要曲線');
hold(app.UIAxes, 'on'); % UIAxesに対してholdを有効にする
red = plot(app.UIAxes, data.Supply, data.Price, 'r-', 'LineWidth', 2, 'DisplayName', '供給曲線');
xlabel(app.UIAxes,'数量(台)');
ylabel(app.UIAxes,'価格(万円)');
legend(app.UIAxes,'需要曲線','供給曲線');
% 軸の設定
ylim(app.UIAxes,[0 160000]); % y軸の範囲を設定
yticks(app.UIAxes,0:10000:160000); % y軸の刻みを設定
yticklabels(app.UIAxes,string((0:10000:160000) .* 1e-4)); % y軸の各刻みの表示文字(単位が万円なので1e-4倍)
xlim(app.UIAxes,[0 6000]); % x軸の範囲を設定
xticks(app.UIAxes,0:1000:6000); % x軸の刻みを設定
end

Accepted Answer

交感神経優位なあかべぇ
こんな感じかな?ってのを作ってみました。
需要供給のグラフですが、X軸とY軸の関係が逆かと思い、入れ替えています。参考までに。
classdef app1 < matlab.mixin.SetGetExactNames % AppDesignerっぽさを出したクラス
% Properties that correspond to app components
properties
UIFigure
UISlider
UIAxes
UIText
end
properties(Access=private) % プライベートプロパティ
blue
red
mark
end
methods
function app = app1()
app.UIFigure = uifigure;
g = uigridlayout(app.UIFigure, 'ColumnWidth',{'1x'}, 'RowHeight',{40,25,'1x'});
% 価格、需要、供給のデータをテーブルとして定義
data = table([10000; 30000; 50000; 70000; 90000; 110000; 130000; 150000], ...
[4000; 3500; 3000; 2500; 2000; 1500; 1000; 500], ...
[700; 1400; 2100; 2800; 3500; 4200; 4900; 5600], ...
'VariableNames', {'Price', 'Demand', 'Supply'});
app.UISlider = uislider(g,"slider","Limits",[min(data.Price), max(data.Price)],"MajorTicks",0:10000:160000,"MajorTickLabels",string((0:10000:160000) .* 1e-4),"ValueChangingFcn",@(src,event) app.UISliderCallback(event));
app.UIText = uilabel(g,"Text","",'HorizontalAlignment','center');
app.UIAxes = uiaxes(g);
% プロット
app.blue = plot(app.UIAxes, data.Price, data.Demand, 'b-', 'LineWidth', 2, 'DisplayName', '需要曲線');
hold(app.UIAxes, 'on'); % UIAxesに対してholdを有効にする
app.red = plot(app.UIAxes, data.Price, data.Supply, 'r-', 'LineWidth', 2, 'DisplayName', '供給曲線');
ylabel(app.UIAxes,'数量(台)');
xlabel(app.UIAxes,'価格(万円)');
% 軸の設定
xlim(app.UIAxes,[0 160000]); % x軸の範囲を設定
xticks(app.UIAxes,0:10000:160000); % x軸の刻みを設定
xticklabels(app.UIAxes,string((0:10000:160000) .* 1e-4)); % x軸の各刻みの表示文字(単位が万円なので1e-4倍)
ylim(app.UIAxes,[0 6000]); % y軸の範囲を設定
yticks(app.UIAxes,0:1000:6000); % y軸の刻みを設定
app.mark = scatter(app.UIAxes,[],[],'MarkerEdgeColor','none','MarkerFaceColor','blue'); % 上にマークの実装が面倒でしたので、とりあえず丸にしました。
legend(app.UIAxes,'需要曲線','供給曲線');
end
function UISliderCallback(app,event) %UISliderのValueChangingFcnのコールバック
blueY = interp1(app.blue.XData, app.blue.YData, event.Value, 'linear', nan);
redY = interp1(app.red.XData, app.red.YData, event.Value, 'linear', nan);
set(app.mark,'XData',event.Value,'YData',blueY);
app.UIText.Text = sprintf('需要台数:%.0f 供給台数:%.0f',blueY,redY);
end
end
end
  1 Comment
みち(プログラミング初心者)
アプリを作成頂きありがとうございます!Chat GPTでは、スクリプトベースでは色々と教えて貰えるのですが、APP全体の構築となると難しく、イメージに近いものを作成頂けて大変有難いです。特に、需要台数と供給台数の計算結果の表示方法については、殆どイメージが湧いていなかったため、とても勉強になりました。お忙しい中ありがとうございましたm(__)m

Sign in to comment.

More Answers (1)

Hiro Yoshino
Hiro Yoshino on 5 Nov 2024
ValueChangedFun ではなくて、ValueChanngingFun でコールバックを設定すると良いかと思います
右クリックで call back の設定から選択可能です。
個人的には、MATLAB による対話型アプリケーションの構築 のトレーニングで徹底的に指導して貰ったので、理解が深まりました。ご興味あれば受講してみるのもアプリの質を上げる一助になるかと。
  1 Comment
みち(プログラミング初心者)
ご回答ありがとうございます!MATLABによる対話型アプリケーションの構築についても、是非受講を検討してみたいと思いました。

Sign in to comment.

Categories

Find more on ビッグ データの処理 in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!