How can I convert a equation into an character array?

Hi community,
I want to convert an equation like: N=1-X into an character array like: Z='N=1-X' or the other way around. The problem is functions like string2num() do not accept variables as input.

댓글 수: 4

Matt J
Matt J 2018년 10월 17일
편집: Matt J 2018년 10월 17일
Convert to a character array from what? In what form does the input equation exist? As a symbolic expression?
I am currently programming a GUI, where I am using in different functions different equations as the z-axis in surf plots. I also need to show the same equation as a label on the z-axis in the surf plot and on a static text field. Therefore I need to convert the function to an character array. To do that I want to use the previously defined equation without typing it all the way again.
Again, as Matt asked, what form does the equation takes in your code? There are many ways to code an equation for plotting and the answer is going to depend on how you've done it.
Emre Sahin
Emre Sahin 2018년 10월 19일
편집: Emre Sahin 2018년 10월 20일
I don't exactly know what you mean, I will just show an example code:
classdef Formfunctions < handle
methods(Static)
function surfplot(handles,X,Y,N)
s=surf(X,Y,N);
s.FaceColor='interp';
a=gca;
a.Box='on';
zlim([0 1]);
xlabel('x')
ylabel('y')
% I want to display the function instead of N %
zlabel('N')
% I want to show the function N on the static text field%
set(handles.text,'string', N)
end
function Node1plot(handles,X,Y)
%Nr.1%
N1=1/4*(1-X).*(1-Y);
figure('Name','Plot Formfunctions: Node1plot',...
'NumberTitle','off', 'Units', 'Normalized', ...
'OuterPosition', [0.5 0 0.5 1]);
colormap(jet)
Formfunctions.surfplot(handles,X,Y,N1)
end
function Node2plot(handles,X,Y)
%Nr.2%
N2=1/4*(1+X).*(1-Y);
figure('Name','Plot Formfunction: Node2plot',...
'NumberTitle','off', 'Units', 'Normalized', ...
'OuterPosition', [0.5 0 0.5 1]);
colormap(jet)
Formfunctions.surfplot(handles,X,Y,N2)
end
end
end
In the main function I will just have the comands:
if(blabla==bla)
Formfunctions.Node1plot(handles,X,Y);
elseif(blabla==blala)
Formfunctions.Node2plot(handles,X,Y);
end
I hope that it is know clear.

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2018년 10월 19일
편집: Matt J 2018년 10월 19일

2 개 추천

    methods(Static)  
        function surfplot(handles,X,Y,fun)  %<---change
            s=surf(X,Y,fun(X,Y));
            s.FaceColor='interp';
            a=gca;
            a.Box='on';
            zlim([0 1]);
            xlabel('x')
            ylabel('y')
            % I want to display the function instead of N %
            N=strrep( func2str(fun) ,'@(x,y)','' );  %<---change
            zlabel( N )
            % I want to show the function N on the static text field%
            set(handles.text,'string', N)
        end
        function Node1plot(handles,X,Y)  
            fun=@(x,y) 1/4*(1-x).*(1-y);  %<---change
            figure('Name','Plot Formfunctions: Node1plot',...
                   'NumberTitle','off', 'Units', 'Normalized', ...
                   'OuterPosition', [0.5 0 0.5 1]);
            colormap(jet)
            Formfunktionen.surfplot(handles,X,Y,fun)  %<---change
        end
     end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2018년 10월 17일

편집:

2018년 10월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by