【问题标题】:How do I get the path from MATLAB?如何从 MATLAB 获取路径?
【发布时间】:2019-11-25 20:05:35
【问题描述】:

我想在 MATLAB 中实现一个 GUI 环境。我想使用浏览器按钮加载文件,然后将文件输入到我要使用的代码中并输出。帮助。

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(~, ~, 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)
[filename, pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname, filename);
text = fileread(fullpathname);
set(handles.text2, 'String', fullpathname)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(~, ~, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname, filename);
text = fileread(fullpathname);
set(handles.text3, 'String', fullpathname)


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(~, ~, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname, filename);
text = fileread(fullpathname);
set(handles.text4, 'String', fullpathname)

D1=load('text2');
D1=D1';
D1=reshape(D1,l1*l2,1);

%% D2,D3 매트릭스 direct데이터 파일

D2=load('text3');
D3=load('text4');

【问题讨论】:

    标签: matlab user-interface matlab-guide


    【解决方案1】:

    你用过

      set(handles.text2, 'String', fullpathname)
    

    所以在需要名字的例程中,可以使用

     get(handles.text2, 'String')
    

    【讨论】:

      【解决方案2】:

      您的所有三个回调都已经完全按照您的意愿进行:

      1. 当用户按下分配了回调之一的按钮时,他们会打开文件资源管理器,用户可以在其中选择文件。
      2. 他们将路径保存在 fullpathname 变量中
      3. 他们将文本分配给句柄text2text3text4String 属性。

      所以现在有几个原因导致它无法按预期工作:

      • 您的任何回调函数都未分配给 GUI 的用户界面元素。如果您在 Matlab GUIDE 功能中打开您的 GUI,您可以简单地检查这一点,右键单击其中一个按钮并检查属性检查器。应该有一个如下所示的条目:

      • 句柄 text2text3text4 都不存在。所以 Matlab 不知道在哪里分配文本。您可以使用上方菜单中的对象浏览器查看所有元素的概览:

      • fileread 无法读取您的文件内容。您可以通过确保 fileread 正在使用简单的非 GUI 脚本处理您的文件来检查这一点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-19
        相关资源
        最近更新 更多