Answered
Set value to an array element if the index exceeds the number of array elements
V = 1:5; k = 6; try N = V(k) catch V(k) = 0; end display(V)

4 years ago | 0

| accepted

Answered
Why can't I run my code
Move the function to the end of the script. The function(s) must come after all other code: https://www.mathworks.com/help/matl...

4 years ago | 0

Answered
How to convert a 4D double matrix into a cell array
A = rand(376,30,20,44); C = num2cell(A,1:3); C = reshape(C,1,[])

4 years ago | 0

| accepted

Answered
how to make an anonymous function with more than 1 statement or equation?
Using anonymous functions: B=2; C=4; Fa = @(x) B.*x; Fd = @(x) C.*x.^2; Fh = @(x) sqrt(Fa(x)+Fd(x)); Fh(2) Using the symb...

4 years ago | 0

| accepted

Answered
How to fprintf a transposed matrix?
fprintf(..,T.'); % ^^ Or skip T entirely: fprintf(..,A); In MATLAB, just like in many other mathematical computati...

4 years ago | 2

Answered
Run multiple scripts in random order
To run scripts you can use RUN: C = {'arrangement1.m', 'arragement2.m', 'arrangement3.m'}; for k = randperm(numel(C)) run...

4 years ago | 1

| accepted

Answered
How to check equality of very small numbers ?
A = 1.309998142150646e-09; B = 1.309998162564509e-09; Method one: compare the absolute difference against a tolerance: tol = ...

4 years ago | 0

| accepted

Answered
How to get Unique values from two columns at a time?
You could use UNIQUETOL, for example: M = [0.075278,0.213,18,127;0.075278,0.220,16,127;0.075278,0.248,31,129;0.075278,0.307,22,...

4 years ago | 0

| accepted

Answered
MD5 hash of .mat files varies
"Is this because of compression when the .mat file is saved?" No, it is beacuse the .mat file includes the date and time when i...

4 years ago | 1

| accepted

Answered
Set multiple fields of a struct to a certain value?
"Set multiple fields of a struct to a certain value?" What your example shows is how to set one field of multiple elements of a...

4 years ago | 0

| accepted

Answered
Error on loop on multiple ASCII files
You should specify the path when importing the file data, for example: P = 'F:\Teamdrive\TEST'; S = dir(fullfile(P,'**','*.asc...

4 years ago | 0

| accepted

Answered
How to sequentially read .txt file to .mat with random names available in a folder
Sorting the filenames into alphanumeric order is not required for your task. The code below: Does not require the subject numb...

4 years ago | 0

Answered
Combining a number and a string array (both columns) into a single column string array.
The simple MATLAB approach: V = linspace(1,10,10); S = "X=" + V(:)

4 years ago | 1

Answered
Using 'Char' Information As A Variable Name
That would be complex and very inefficient. Your suggested approach is best avoided. A much simpler approach is to store the im...

5 years ago | 0

Answered
Use string name as variable name for struct
D.A__B__C = 0.5; D.A__B__D = pi F = fieldnames(D); C = regexp(F,'_+','split'); Z = struct(); for k = 1:numel(F) Z = se...

5 years ago | 0

| accepted

Answered
How to import data of the form [variable name]Data[-]
rgx = '\[(\w+)\]\s*([^\[]+)\[\-\]'; str = fileread('test.txt'); tkn = regexp(str,rgx,'tokens'); tkn = vertcat(tkn{:}); tmp =...

5 years ago | 1

| accepted

Answered
Why values get changed while doing indexing?
"Why values get changed while doing indexing?" They don't. The values are exactly the same, they are simply displayed slightly...

5 years ago | 0

| accepted

Answered
How to create a matrices using while loop?
r = 0.1; y = 2.5; i = 0; a = []; % <--- while i<=5 i=i+1; dis = r.*sin(y.*i); tim = dis*2; a = [a;i,dis,...

5 years ago | 1

| accepted

Answered
Sort repetition in text
T = 'KKKKLLOOKkl'; D = [true,diff(T)~=0]; L = diff(find([D,true])); C = compose('%s%d',T(D).',L(:))

5 years ago | 1

Answered
Turn table columns into rows
https://www.mathworks.com/help/matlab/ref/rows2vars.html

5 years ago | 0

| accepted

Answered
Move an element in a vector
V = 1:5; N = numel(V); A = repmat(1:N,N,1); [~,X] = sort(tril(1+A,-1)+eye(N)+triu(A,1),2); Z = V(X)

5 years ago | 0

| accepted

Answered
Loop through multiple levels of subfolders to run .mat files
P = 'absolute/relative path to the main directory'; S = dir(fullfile(P,'**','*.mat')); for k = 1:numel(S) F = fullfile(S(...

5 years ago | 0

| accepted

Answered
How to extract numbers from a string between a special character?
str1 = 'aa_100_6'; str2 = 'aa_1000_10'; str2double(regexp(str1,'\d+','match')) str2double(regexp(str2,'\d+','match')) Or if ...

5 years ago | 0

Answered
How to use a filename character array as a name of a variable?
A very neat and simple approach is to import the data into the same structure that DIR returns: P = 'absolute or relative path ...

5 years ago | 0

Answered
elegant way to initialize or add on end of a variable
numbers = 100*rand(100,1); high_numb = []; % empty! low_numb = []; % empty! for i=1:1:size(numbers,1) if numbers(i)>99 ...

5 years ago | 0

| accepted

Answered
Table Assignment using two tables of different height.
You can use ISMEMBER like this: T1 = array2table([8,5;9,9;2,9;9,2;6,9;1,9;3,5]) T2 = array2table([6,0;7,0.1;8,0.2;9,0.3]) [X,...

5 years ago | 0

| accepted

Answered
Reading the last line of each vector in a cell
After the loop, all at once: fnh = @(m)m(end,1:5); tmp = cellfun(fnh,Data,'uni',0); out = vertcat(tmp{:}) Or individually: ...

5 years ago | 0

Answered
How to merge multiple structures into one structure or a table?
The MATLAB approach, these are all equivalent: merged = [result1.values]; merged = cat(2,result1.values); merged = horzcat(re...

5 years ago | 0

| accepted

Answered
For Loop that Checks 2 Matrices to create a Combined New one based on IF Statement
Here is a direct translation of your Excel formula: T = readtable('Data.xlsx') % import your data nmr = height(T); out = zero...

5 years ago | 0

| accepted

Answered
What is the meaning of "Trailing string must be 'omitan' or 'includenan' " on MATLAB. I run the programm below but this error message displays
The 'all' option is not available in your version (R2016a). It was introduced in R2018b: https://www.mathworks.com/help/matlab/...

5 years ago | 1

| accepted

Load more