145 views (last 30 days)
Show older comments
恩兴 on 24 Sep 2024 at 2:27
Edited: Stephen23 on 25 Sep 2024 at 3:02
Open in MATLAB Online
Hi there! My problem is I want to clear my work space at beginning. So I use func "clear" also with "clc" and "close all". But after running the programme, there is no variables in workspace. When I remove "clear", they appear again! How can I fix it?
clc; clear; close all;
img = imread("Lena.tif");
13 Comments Show 11 older commentsHide 11 older comments
Show 11 older commentsHide 11 older comments
Epsilon on 24 Sep 2024 at 2:54
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268160
Hi,
This should not be the case. Can you please share more data to help us help you.
Aquatris on 24 Sep 2024 at 6:41
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268250
clear command deletes everything in Matlab workspace. So if you run the shown code, only 'img' variable will be present in your workspace. What exactly are you trying to do?
Piyush Kumar on 24 Sep 2024 at 7:41
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268320
@恩兴, "I want to clear my work space at beginning." - Using "clear;" at the beginning should be sufficient for this.
Please share the program that you are running.
恩兴 on 24 Sep 2024 at 8:58
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268360
actually, these is all the code. The problem is, after i run the code, even "img" will not appear in workspace. I use "clear" to clear variables from last run, but it seems to clear variables of current run.
DGM on 24 Sep 2024 at 9:19
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268375
Open in MATLAB Online
If you issue
whos img
after executing your script, does it return anything? The hypothesis here is that the workspace viewer might simply not be updating.
Alternatively, if your console is hidden, you might not be seeing an error which prevents the call to imread() from completing successfully.
Stephen23 on 24 Sep 2024 at 9:26
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268380
Edited: Stephen23 on 24 Sep 2024 at 9:31
"But after running the programme, there is no variables in workspace. When I remove "clear", they appear again!"
After running the code you show, I would expect only one variable. Why do you use the plural "they" ? How many variables do you expect to see in the workspace?
Piyush Kumar on 24 Sep 2024 at 9:34
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268395
Open in MATLAB Online
I could not reproduce the situation you are describing. For me, "img" is present in the workspace even after the execution completes. Just sharing an example using who command -
clear;
a=5;
who
Your variables are:a
恩兴 on 24 Sep 2024 at 10:02
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268420
Edited: 恩兴 on 24 Sep 2024 at 10:55
Open in MATLAB Online
@Piyush Kumar@DGM@Epsilon@Aquatris
Thanks. It seems that I have found where the problem is. Here is my folder:
My code is:
clc; clear; close all;
%%
addpath("func\");
addpath("images\");
img = imread("Lena.tif");
At this time, after I run it, there is nothing in workspace, but using "who", variables do exist, just like:
If I delet
addpath("func\");
after running, "img" appears in workspaces.
I dont know why this would happen.
Stephen23 on 24 Sep 2024 at 10:54
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268435
Edited: Stephen23 on 24 Sep 2024 at 11:16
Do not change the MATLAB Search Path in order to access data files.
The recommended approach for importing/exporting data files is to use an absolute/relative filename.
FULLFILE is a very useful tool for generating absolute/relative filenames.
Aquatris on 24 Sep 2024 at 11:52
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268525
I can't seem to reproduce what you experience in Matlab 2019b or 2022b.
I created a dummy Lena tif file, func and images folder, ran your code and I always see img variable in workspace. Are you sure your script actually runs fully without any errors and stops?
Cris LaPierre on 24 Sep 2024 at 13:54
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3268615
Edited: Cris LaPierre on 24 Sep 2024 at 14:06
Have you tried restarting MATLAB?
What are the contents of your folder func? I wonder if something in there is shadowing a MATLAB function and therefore leading to the behavior you are observing. This would potentially explain why we can't duplicate the issue.
恩兴 on 25 Sep 2024 at 1:17
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3269015
Open in MATLAB Online
Ok, there is just a normal function in func. I finally fix this problem by using code below:
clc; clear; close all;
%%
addpath(".\func\");
% addpath(".\images\");
img = imread(".\images\Lena.tif");
Everything seems ok now. I decide to use relative filename future. Thanks a lot.
@Stephen23@Aquatris@Cris LaPierre
Stephen23 on 25 Sep 2024 at 3:01
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running#comment_3269035
Edited: Stephen23 on 25 Sep 2024 at 3:02
Get rid of ADDPATH. The whole point of using an absolute/relative filenames is that you do not need to (and should not) change the Search Path to access data files.
Sign in to comment.
Sign in to answer this question.
Answers (0)
Sign in to answer this question.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office