필터 지우기
필터 지우기

Why doesn't Matlab know the variable from appdesigner despite it is globalized into matlab workspace ?

조회 수: 3 (최근 30일)
Hi All
I have written an app that using a pushbutton receives the working directory folder adress from the user and changes the matlab wd to that. the other push button is to run an mfile in thatdirectory and then cd() to some subfolder.
Despite I see the global variable shared between app and workspace ,it seems that matlab can not use it and therefore the cd() does not work cause the variable is seen as empty despite it's not
here is the app code :
properties (Access = public)
%selectedPath: path selected by user in ButtonPushed function
selectedPath= 'C:\' % Description
end
methods (Access = private)
% Button pushed function: ChooseDirectoryButton
function ChooseDirectoryButtonPushed(app, event)
global currentFolder
app.selectedPath = uigetdir();
app.UIFigure.Visible = 'on';
currentFolder=app.selectedPath;
cd(currentFolder)
end
% Button pushed function: ExecuteButton
function ExecuteButtonPushed(app, event)
global currentFolder;
currentFolder=app.selectedPath;
assignin('base','currentFolder',currentFolder);
goFolder = strcat(currentFolder,'\Fsolver.m');
run(goFolder)
app.UIFigure.Visible = 'on';
end
end
and the first lines of the mfile :
global currentFolder
inputs =strcat(currentFolder,'\Input')
cd(inputs)
these are the errors :
Error using cd
Cannot CD to \Input (Name is nonexistent or not a directory).
Error in mysolver (line 37)
cd(inputs)
Error in run (line 91)
evalin('caller', strcat(script, ';'));
Error in apprivate/ExecuteButtonPushed (line 39)
run(goFolder)
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 309)
Error while evaluating Button PrivateButtonPushedFcn.
  댓글 수: 4

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

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 3월 19일
farzad - rather than using a global variable, can't you just use pwd to identify the current working directory? So the code in your m file would then become
inputPath = fullfile(pwd, 'Input')
cd(inputPath)
where fullfile is used to build the path. Of course the problem here is on the next line you will changed directory to that folder. Is that necessary? Could you just use inputPath for any code that tries to read or write to file?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by