Change a Variable's Name in Matlab's Workspace - matlab

I have a problem that has more than likely been asked before but I have yet to find a solution that works for me.
I would like to know if it's possible to change the name of variable within a workspace in Matlab?
The need for this is a follows,
I have a small section of code that imports over 60's columns of data from Excel into Matlab, each column has over 10,000 rows in, so a fair amount of data. Row 1 of each column is the variable, i.e. R1C1 = "Time from X to Z" I would like to know if its possible for Matlab to create a Variable called "Time_from_X_to_Z" then store all the data in that Variable from the excel sheet. I am able to store all the data in separate variables, I just need a bit of help naming the variable.
I would like to name the variables like so, as when I come to re-use the code the order of the column may change from time to time, hence why I can't just let Variable1 = Varibale1 etc.
Here is some of the code I have tried from my research.
VariableName = txt(1,i);
VariableName = num2str(cell2mat(VariableName));
VariableName = Variable
clear Variable
This was my most successful one but is nowhere near what I expected it to be.
I hope that all makes sense.
Thanks for any help you are able to provide.

Related

NMISS on all variables

I am trying to count the missings in my dataset, but I have a problem.
My first question is named Q2, and the last one is Q55A7. Thus, I cannot use NMISS(Q2 to Q55A7). is there no way to do it by the ID instead of the name? Or how can I get this working?
Actuall NMISS(Q2 to Q55A7) shouls work fine - when you are referring to variables in the data with the to keyword SPSS doesn't look at the structure of the name, it just takes all the variables in the data that are between these two, according to their order in the data.
The problem you may run into is with the existance of other variables that you don't want included (e.g. an open ended question in between the other questions. Here are a couple of ways to work around that -
Let's say your variables are ordered as follows: Q2 Q3 Q4 Q4Other Q5 Q6 Q55A7. Using Q2 to Q55A7 will incluede Q4Other which is an open ended text variable which you can't include in the calculation. In this case you can use:
nmiss(Q3 to Q4, Q5 to Q55A7)
Now if there are many more variables, and many variables you need to skip, the above method becomes as bothersome as stating all the variables' names. Another way to go back to nmiss(Q3 to Q55A7) is by changing the order of the variables so only the numeric variables you want in the analysis are actually placed between these two. One way to do that is:
add files /file=* /keep Q4Other Q17Other SomeOtherOpenQ all.
Rrunning this will bring all the specified variables to the beginnig of the dataset, leaving only the variables you want in the analysis between Q3 to Q55A7.
If you still want to inprove on that, you can look up spssinc select variables (see my answer here for example) extention command which enables you to define variable lists according to their attributes, and then you can run the analysis on a list of variables.

Exploit Matlab copy-on-write by ensuring function arguments are read-only?

Background
I'm planning to create a large number of Matlab table objects once, so that I can quickly refer to their contents repeatedly. My understanding is that each table variable/column is treated in copy-on-write manner. That is, if a table column is not modified by a function, then a new copy is not created.
From what I recall of C++ as of 1.5 decades ago, I could ensure that the code for a function does not modify its argument's data by using constant-correctness formalism.
The specific question
I am not using C++ in these days, but I would like to achieve a similar effect of ensuring that the code for my Matlab function doesn't change the data for selected arguments, either inadvertently or otherwise. Does anyone know of a nonburensome way to do this, or just as importantly, whether this is an unrealistic expectation?
I am using R2015b.
P.S. I've web searched and came across various relevant articles, e.g.:
http://www.mathworks.com/matlabcentral/answers/359410-is-it-possible-to-avoid-copy-on-write-behavior-in-functions-yet
http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data
(which I need clarification on to fully understand, but it isn't my priority just now)
However, I don't believe that I am prematurely optimizing. I know that I don't want to modify the tables. I just need a way to enforce that without having to go through contortions like creating a wrapper class.
I've posted this at:
* Stack Overflow
* Google groups
There is no way of making variables constants in MATLAB, except by creating a class with a constant (and static?) member variable. But even then you can do:
t = const_table_class.table;
t(1,1) = 0; % Created and modified a copy!
The reason that a function does not need to mark its inputs as const is because arguments are always passed by value. So a local modification does not modify data in the caller’s workspace. const is something that just doesn’t exist in the MATLAB language.
On the other hand, you can be certain that your data will not be modified by any of the functions you call. Thus, as long as the function that owns the tables does not modify them, they will remain constant. Any function you pass these tables to, if they attempt to modify them, they will create a local copy to be modified. This is only locally a problem. The memory used up by this copy will be freed upon function exit. It will be a bug in the function, but not affect code outside this function.
You can define a handle class that contains a table as it's preperty. Define a property set listener that triggers and generates error/warning when the value of the property changes.
classdef WarningTable < handle
properties (SetObservable)
t
end
methods
function obj = WarningTable(varargin)
obj.t = table(varargin);
addlistener(obj,'t','PreSet',...
#(a,b)warning('table changed!'));
end
end
end
This should generate warning:
mytable = WarningTable;
mytable.t(1,1) = 0;

LibreOffice, Using Constants as query Parameters

I'm using the VLOOKUP function to move data from one table into another. I need to apply this formula to an entire column, and I need to know how to define certain parameters as variable and some as constant.
Here's my problem:
=VLOOKUP($D8,Sheet2.A1:B20,2)
becomes, when I drag the corner of the cell across multiple rows,
=VLOOKUP($D8,Sheet2.A1:B20,2)
=VLOOKUP($D9,Sheet2.A2:B21,2)
=VLOOKUP($D10,Sheet2.A3:B22,2)
=VLOOKUP($D11,Sheet2.A4:B23,2)
And what I need is
=VLOOKUP($D8,Sheet2.A1:B20,2)
=VLOOKUP($D9,Sheet2.A1:B20,2)
=VLOOKUP($D10,Sheet2.A1:B20,2)
=VLOOKUP($D11,Sheet2.A1:B20,2)
With the first parameter changing and the rest remaining constant. I'm sure there is an easy way to do this, but searching and browsing help topics is returning nothing. I admittedly have zero background in spreadsheets. Thanks for your help
Add more $ signs, like this:
=VLOOKUP($D8,Sheet2.$A$1:$B$20,2)
https://help.libreoffice.org/Calc/Addresses_and_References,_Absolute_and_Relative

dynamic variable names in matlab

I wish to expand a structure (bac) with a number of fields from another structure (BT). The names of these fields are contained in the cell array (adds) as strings.
this is what i have now (and obviously doesn't do the job, explaining this post):
for i=1:numel(adds)
eval(genvarname('bac.',adds{i})) = eval(strcat('BT.',adds{i}));
end
I also tried using sprintf, which did not seem to work for me. I feel confident one of you knows how to do it, since I feel it should be rather easy.
The best way of doing this is to use dynamic field names:
for i=1:numel(adds)
bac.(adds{i}) = BT.(adds{i});
end

automatically get the name vectors saved in the workspace in Matlab

I have multiple vectors (+100) that have been loaded into MATLAB workspace, I would like to write a script that can plot and save them all, but for that I need their name, my question is: is there a way to automatically get the name vectors saved in the workspace.
thanks in advance.
Step one: whoever gave you a *.mat file with 100+ named variables in it, [censored for strong language and scenes some viewers may find upsetting]. I am only partly joking here; if you find yourself in this sort of situation normally it is because something has gone terribly wrong upstream. We can work around it, though.
Step two: use who with the filename to get a list of variables in that file
names = who('-file', 'all');
Step three: load the variables (or a subset of them) into a struct
data = load('all.mat');
Step four: use dynamic structure naming to extract data:
for n = 1:length(names);
plot(data.(names{n})); % or whatever you want to do with this data
end
I would probably just use the loop to dump the data in a cell array so as to make further processing simpler and avoid further use of dynamic field names or worse, eval.
You can use who, which lists all variables alphabetically in the active workspace.