after I use "clear" at beginning, there is no variables in workspac... (2024)

145 views (last 30 days)

Show older comments

恩兴 on 24 Sep 2024 at 2:27

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2154965-after-i-use-clear-at-beginning-there-is-no-variables-in-workspace-after-running

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

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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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:

after I use "clear" at beginning, there is no variables in workspac... (10)

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:

after I use "clear" at beginning, there is no variables in workspac... (11)

after I use "clear" at beginning, there is no variables in workspac... (12)

If I delet

addpath("func\");

after running, "img" appears in workspaces.

after I use "clear" at beginning, there is no variables in workspac... (13)

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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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.

See Also

Categories

MATLABProgrammingFiles and FoldersSearch Path

Find more on Search Path in Help Center and File Exchange

Tags

  • clear
  • workspace

Products

  • MATLAB

Release

R2023a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


after I use "clear" at beginning, there is no variables in workspac... (19)

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

after I use "clear" at beginning, there is no variables in workspac... (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 6538

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.