Show Only certain variables in the workspace

21 views (last 30 days)
Alex
Alex on 30 Aug 2016
Commented: Steven Lord on 2 Jul 2024
Hi everybody,
in my scripts I have defined a lot of variables, matrices, vectors and structs. These appear all in the workspace. Now I want to display only specific variables,to have a better overview.
I know, i should work from the beginning with functions instead of Script but i didn´t know it. I tried to rewrite scripts in functions, but it so complicated and im afraid that my Code wil not work after i edit it.
i will appreciate any Help
  2 Comments
Stephen23
Stephen23 on 30 Aug 2016
Edited: Stephen23 on 30 Aug 2016
That is part of the learning process: you learn a better way of doing something, and then you have a choice: to keep doing things the old (slow, inefficient, buggy) way, or rewrite everything and do it a better way.
Functions are much better than scripts for real code, they make managing data much easier, as well as being easier to write, to test, to fix, to understand... why would you even want to keep using scripts?
Rewrite your code to use functions properly and you will not regret it.
prabhat kumar sharma
prabhat kumar sharma on 2 Jul 2024
If you still wants a hack as an immediate solution , you can try storing all the variables in a .mat file and later clear your workspace , and after that you can load only the necessary variables.
Step 1: Save all your variables in a mat file.
step 2:
clear;
Step 3:
% Load specific variables from a .mat file
data = load('yourfile.mat', 'var1', 'var2', 'var3');

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 2 Jul 2024
Edited: John D'Errico on 2 Jul 2024
The point of what @Stephen23 and I are trying to tell you is that you are asking the wrong question. You want someone to teach you how to hack things, so that you can continue to do your work the wrong way, making more and more complex scripts, without ever wanting to take the important step, and learn to use functions.
And yes, it may seem a hurdle to overcome, that moving to functions from scripts is a hard thing to do. But it will get only worse. Your problems will compound exponentially as you keep adding things to those scripts. New scripts will create new variables that stay around and mess things up. If it is complicated now, it will only be far more complicated tomorrow. as it is, you are already nearing the tipping point, where you are finding your code is becoming an unworkable mess.
At some point you will end up with a truly unworkable mess that even you cannot understand and debug. I say debug because even though you are worried about functions making things difficult to debug, they will in fact make things far more easy to debug.
Write ONE function. Just something small, for one small piece of your code. Test it. Verify that it does what it should do. Do you see that you can test a function on its own? Once you know that function does the job it is intended to do, go on to the rest. Do one at a time. Remember this: you eat a programming elephant one byte at a time.
When you have broken down all of your code into functions, combine them. This step is easy to do, since all you need do is combine a few function calls.
The best time to fix a problem is to start today. Yesterday was better yet, but it is too late for that now.
  1 Comment
Steven Lord
Steven Lord on 2 Jul 2024
I second John's suggestion, especially about testing the code to make sure it's doing what you want it to do (and not doing what it shouldn't be doing.) [Of course, I am biased in favor of testing in general because of my job, but that doesn't make me wrong.]
I know that you may say "But writing tests is difficult!" But in MATLAB, it's actually easier than you might think. You can write test scripts that you can run using the testing infrastructure included in MATLAB; see the example on this documentation page. As long as you know the assert function (or I believe you could use the error function instead in an if, elseif, else block) you can write a test.
As you become more comfortable with the state of your system you may decide you want to use some of the additional functionality that function-based tests or class-based tests provide (at the expense of some additional complexity.) Or you may stick with script-based tests. That's perfectly fine too.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!