how to use iteration variable in variable name - Matlab [duplicate] - matlab

This question already has answers here:
Matlab; Structure field name with valuable ( = number)
(2 answers)
Create structure fieldnames from array of numbers
(1 answer)
Closed 10 months ago.
I have a struct res with the variables: det1, det2, det3.
I also have the amount of these variables (in this case 3), and I want to read them in a loop.
How can I integrate the iteration variable in the name of the variables in the loop and read them?
I want to do something like this:
for i=1:num_variables
var_name = 'res.det'+num2str(i);
% read var_name
end
Thanks for the help!

Related

Powershell how to replace multiple commands with output of single variable [duplicate]

This question already has answers here:
Access PSObject property indirectly with variable
(3 answers)
Set Value of Nested Object Property by Name in PowerShell
(3 answers)
Closed 2 months ago.
I am traversing an object and would like to replace several path parts with a string stored in another variable. For example:
# a is a complex ps object, the path is valid
$a.b.c
myvalue
$replace = "b.c"
$a.$($replace)
# here I expect 'myvalue', however nothing is returned, as I would like to execute the equivalent of $a.b.c
Is there some way to do this replacement?

Concat variable's name in powershell [duplicate]

This question already has answers here:
Dynamically create variables in PowerShell
(1 answer)
Create an incrementing variable from 2 variables in PowerShell
(2 answers)
Closed 10 months ago.
I looking for this in powershell
$VAR0=args[0]
$VAR1=args[1]
$VAR2=args[2]
With a construction like this
$a=0
$VAR+$a=args[$a]
but I cannot concatenate this way

how do i get a variable from a seperate text file [duplicate]

This question already has an answer here:
Import Variables from Text File in Powershell
(1 answer)
Closed 6 years ago.
Lets say I have a text file full of variables.
I would like to create a separate file in which I can read and call these variables using powershell.
EX. Variable.txt contains $X=10
Main.ps1 retrieves the variable X from the text file, and doubles it.
Apologize is this is an amateur question, I am very new to powershell.
For your specific purpose, you could have a file, data.ps1 containing your data, like so:
$x=10
Then, a script.ps1 file, similar to the following:
. ./data.ps1
$x = $x * 2
Write-Host "Value X is $x"
Which would give you the following output when you run ./script.ps1:
Value X is 20

How to redirect Matlab output of a command to a file? [duplicate]

This question already has answers here:
Print A Matlab Struct to a Text File
(4 answers)
Closed 9 years ago.
I want to redirect or copy the output of a Matlab command to a file. How can I do that?
In my case, I have two large structs that I want to compare using the UNIX tool diff.
Example: I can do this in Matlab:
>> s1
s1 =
a: 32
abc: 'example'
>>
and want a file containing approx:
s1 =
a: 32
abc: 'example'
These solutions are not viable:
Copy-pase: can't automate (comfortably).
save -ascii: does not work with structs.
Have a look at the diary function. E.g.
diary my_file.txt
s1
diary off
The file my_file.txt will then contain exactly what you see on screen.
If you need to do it more fine grained there is the evalc function that will store the output to a string.
Later you can output the string into any output channel matlab offers.

Opening multiple files with similar names in MATLAB: importdata cannot open the files given the name by strcat or dir [duplicate]

This question already has answers here:
How can I load 100 files with similar names and/or string in just one step in MATLAB?
(3 answers)
Closed 9 years ago.
As Shai and CTZStef suggested, when I have to open multiple files with similar names in MATLAB, I have to do
for k=1:size(fls)
fileName = strcat('int_',num2str(k),'.ASC');
A(k) = importdata(fileName, ' ', 9);
x(k) = A(k).data(:,1);
y(k) = A(k).data(:,2);
end
or also
fls = dir('int_00_*.ASC');
for fi=1:numel(fls)
A(fi) = importdata(fls(fi).name, ' ',9);
end
Well, the problem is that none of them works.
What should I do? Is it a problem with my MATLAB version?
You need to follow the answers you got more closely:
The strcat solution CANNOT handle the zero padding of the file names. You'll ahve to manually rename all files from 'int_001.ASC' to 'int_1.ASC'.
UPDATE (due to #DedekMraz's comment): you'll need to modify the strcat input string to strcat('int_', num2str( k, '%03d' ), '.ASC');
You can use a strategy similar to this one. See the update to the answer you already got.
The input you gave your dir function is wrong it has to be dir('int_*.ASC') and not dir('int_00_*.ASC').