声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2511|回复: 14

[绘图技巧] 结果显示和图象显示问题

[复制链接]
发表于 2007-5-28 16:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
我编写了一个截面,在指定位置输入数据,便在相应的位置显示结果和图象,程序如下:
function varargout = jiangxiaohuai(varargin)
% JIANGXIAOHUAI M-file for jiangxiaohuai.fig
%      JIANGXIAOHUAI, by itself, creates a new JIANGXIAOHUAI or raises the existing
%      singleton*.
%
%      H = JIANGXIAOHUAI returns the handle to a new JIANGXIAOHUAI or the handle to
%      the existing singleton*.
%
%      JIANGXIAOHUAI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in JIANGXIAOHUAI.M with the given input arguments.
%
%      JIANGXIAOHUAI('Property','Value',...) creates a new JIANGXIAOHUAI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before jiangxiaohuai_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to jiangxiaohuai_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help jiangxiaohuai

% Last Modified by GUIDE v2.5 26-May-2007 20:12:15

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @jiangxiaohuai_OpeningFcn, ...
                   'gui_OutputFcn',  @jiangxiaohuai_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before jiangxiaohuai is made visible.
function jiangxiaohuai_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to jiangxiaohuai (see VARARGIN)

% Choose default command line output for jiangxiaohuai
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes jiangxiaohuai wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = jiangxiaohuai_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

k2=str2num(get(findobj('tag','edit1'),'string')); %从edit9中读取数字
k4=str2num(get(findobj('tag','edit2'),'string')); %从edit10中读取数字
k5=str2num(get(findobj('tag','edit3'),'string'));  %从edit11中读取数字
u=str2num(get(findobj('tag','edit4'),'string'));   %从edit5中读取数字
index=get(handles.popupmenu1,'value'); %从popupmenu1中选择变量
p=sym(get(findobj('tag','edit6'),'string'));
switch  index
    case 1
        g=16e-6;
    case  2
        g=64e-6;
    case 3
        g=256e-6;
    case  4
        g=1024e-6;
     case 5
        g=4096e-6;
    case  6
        g=16384e-6;
end
t=(k2+k4+k5)*u*g*p;
x=int(t,0,35);
answer=char(p);
set(handles.edit5,'string',num2str(x)); %在edit1中显示运算结果
axes(handles.axes1);
cla;
ezplot(p,t);
hold on;

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double
 
显示的错误为
??? Error using ==> fix
Function 'fix' is not defined for values of class 'sym'.

Error in ==> C:\MATLAB6p5p1\toolbox\matlab\strfun\num2str.m
On line 32  ==> if nargin < 2 & ~isempty(x) & all(all(x==fix(x)))

Error in ==> C:\MATLAB6p5p1\work\jiangxiaohuai.m (pushbutton1_Callback)
On line 105  ==> set(handles.edit5,'string',num2str(x)); %在edit1中显示运算结果

Error in ==> C:\MATLAB6p5p1\toolbox\matlab\uitools\gui_mainfcn.m
On line 66  ==>         feval(varargin{:});

Error in ==> C:\MATLAB6p5p1\work\jiangxiaohuai.m
On line 42  ==>     gui_mainfcn(gui_State, varargin{:});

??? Error while evaluating uicontrol Callback.

界面见附件
result后显示x的运算值,axes中显示t=(k2+k4+k5)*u*g*p的曲线
谢谢大家.
未命名1.bmp
回复
分享到:

使用道具 举报

发表于 2007-5-28 16:42 | 显示全部楼层
原帖由 laojiang 于 2007-5-28 16:28 发表
我编写了一个截面,在指定位置输入数据,便在相应的位置显示结果和图象,程序如下:
function varargout = jiangxiaohuai(varargin)
% JIANGXIAOHUAI M-file for jiangxiaohuai.fig
%      JIANGXIAOHUAI, b ...



符号变量与数值型变量的转换问题,建议搜索一下版面的相关帖子
 楼主| 发表于 2007-5-28 17:11 | 显示全部楼层

回复 #2 eight 的帖子

我把'set(handles.edit5,'string',num2str(x)); %在edit1中显示运算结果'这句去掉后,在axes中能显示图象,x在工作界面上也有结果显示,就是不能在edit5中显示出来,应该是eight兄提出的问题吧,可是我看了很多例子,都没有找出解决的方法,望指教.谢谢您!!!
发表于 2007-5-28 17:29 | 显示全部楼层
原帖由 laojiang 于 2007-5-28 17:11 发表
我把'set(handles.edit5,'string',num2str(x)); %在edit1中显示运算结果'这句去掉后,在axes中能显示图象,x在工作界面上也有结果显示,就是不能在edit5中显示出来,应该是eight兄提出的问题吧,可是我看了很 ...


试试:set(handles.edit5,'string',num2str(subs(x)));

评分

1

查看全部评分

 楼主| 发表于 2007-5-28 17:54 | 显示全部楼层

回复 #4 eight 的帖子

问题果然得到解决,subs的用法在那里能查到啦,它的含义是什么呀??以后得多向你们高手学习,在某些方面比自己看书来得快.再求教一个问题我在guid界面中axes中绘图时,我在它的属性中设定了axes的两坐标分别为x=[0,35],y=[0,1],结果在界面运行后,axes的坐标发生了变化,变成了[0,6]和[-3,3],谢谢你,你们无私的给人排解问题,我很感动.
发表于 2007-5-28 18:24 | 显示全部楼层
原帖由 laojiang 于 2007-5-28 17:54 发表
问题果然得到解决,subs的用法在那里能查到啦,它的含义是什么呀??以后得多向你们高手学习,在某些方面比自己看书来得快.再求教一个问题我在guid界面中axes中绘图时,我在它的属性中设定了axes的两坐标分别为 ...



1. help subs
2. 一般不会遇到这个情况,你是使用 fig 文件方式的 GUI 设计吗?即通过 guide 面板来放置控件?你指的是 position 属性的 x、y 坐标吗
 楼主| 发表于 2007-5-28 20:39 | 显示全部楼层

回复 #6 eight 的帖子

1,我在书中查到了subs,是符号转换的的作用,但是在这里的具体作用不是很了解.
2,我是在xlim和ylim中设定的坐标的长度,即x为(0--35),y为(0--2),不是在position中设置的,但是程序运行后,结果发现axes的长度改变了,不知道是怎么回事.谢谢
发表于 2007-5-28 20:42 | 显示全部楼层
doc axes,查看坐标轴的属性,比如说square;equal
发表于 2007-5-28 20:51 | 显示全部楼层
原帖由 laojiang 于 2007-5-28 20:39 发表
1,我在书中查到了subs,是符号转换的的作用,但是在这里的具体作用不是很了解.
2,我是在xlim和ylim中设定的坐标的长度,即x为(0--35),y为(0--2),不是在position中设置的,但是程序运行后, ...


先画图,后设置
 楼主| 发表于 2007-5-29 08:37 | 显示全部楼层

回复eight兄和spano

我是这样做的,在xlimmode中选择manual模式,就可以人工设置x和y的上下限,但是当运行后上下限发生改变,变成了变成了[0,6]和[-3,3]
运行前后的界面见附件1和2

[ 本帖最后由 eight 于 2007-5-29 09:48 编辑 ]
未命名.bmp
未命名.bmp
发表于 2007-5-29 09:50 | 显示全部楼层
原帖由 laojiang 于 2007-5-29 08:37 发表
我是这样做的,在xlimmode中选择manual模式,就可以人工设置x和y的上下限,但是当运行后上下限发生改变,变成了变成了[0,6]和[-3,3]
运行前后的界面见附件1和2


不需要设置,在画图前添加一个语句就可以了:axis([0,35,0,2]);
 楼主| 发表于 2007-5-29 10:56 | 显示全部楼层

回复 #11 eight 的帖子

我尝试过了,出现的是相同的结果,
1,在属性中设置上下限
2,属性中设置和axis([0,35,0,2]);语句的同时套用
3,单独在主程序中键入axis([0,35,0,2]);
它们出现的结果是一样的,是不是我的版本太低的缘故呀,我的是6.5.1的.
发表于 2007-5-29 11:05 | 显示全部楼层
原帖由 laojiang 于 2007-5-29 10:56 发表
我尝试过了,出现的是相同的结果,
1,在属性中设置上下限
2,属性中设置和axis([0,35,0,2]);语句的同时套用
3,单独在主程序中键入axis([0,35,0,2]);
它们出现的结果是一样的,是不是我的版本太低的缘故 ...


估计与版本无关。我下午回实验室试试吧,这里没有安装 matlab
发表于 2007-5-29 15:18 | 显示全部楼层
代码:

  1. x = 0:0.01:2*pi;
  2. y = sin(x);
  3. plot(x,y);
  4. axis([1,2,-1,1])
  5. hold on;
  6. xx = 1.2:0.01:3*pi;
  7. yy = cos(xx);
  8. plot(xx,yy,'r');
复制代码
结果:
untitled.bmp
说明:先作图,再设置,此时坐标轴经过用户设置后能保持不变,对之后作的图也生效

评分

1

查看全部评分

 楼主| 发表于 2007-5-29 16:16 | 显示全部楼层

回复 #14 eight 的帖子

问题得到解决,谢谢eight兄,我发现在guid界面中,axes的坐标长度是有规定的,比如x=(0-10),y=(0-2),那么x轴的长度为y轴的5倍长,谢谢你!!!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-25 06:54 , Processed in 0.075299 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表