Answered
How to naming for this? MATLAB Question
Numbering variables like that is not a good way to write MATLAB code. You could do that, but you force yourself into writing slo...

6 years ago | 0

| accepted

Answered
Take string input and reformat as cell array
>> str = fileread('SampleData.txt'); >> str = regexprep(str,{'",','[":{}\[\]]+'},{' ',''}); >> fmt = '%*s%{yyyy-MM-dd''T''HHmm...

6 years ago | 0

| accepted

Answered
How to name each element in a dataset after their row and column names?
>> M = randi(9,4,4) % random data M = 7 5 6 1 5 9 2 2 3 7 5 8 9 8 7 9 >> X = triu(true...

6 years ago | 1

| accepted

Answered
'Matrix dimensions must agree' error on string operations
"...don't really know what's wrong." What you wrote performs an element-wise comparison of the character in two character array...

6 years ago | 0

| accepted

Answered
How to calculate the syms with variable has the same name?
function out = myfun(exp,a,b) fun = str2func(sprintf('@(a,b)%s;',exp)); out = fun(a,b); end Tested: >> myfun('a>0',3,2) an...

6 years ago | 0

Answered
I am trying to create a loop that will be able to automatically follow a certain order of operations.
>> C = {'TP','TP','TP','TP','NaN','NaN','Px'}; >> [~,idx] = sort(strcmpi(C,'NaN')) idx = 1 2 3 4 7 5 6 and th...

6 years ago | 1

| accepted

Answered
create vector from each element it had
Method one: nonzeros: vec = [x,y,z]; tmp = 1:max(vec); new = nonzeros((tmp(:)<=vec).*tmp(:)).' % requires >=R2016b Method tw...

6 years ago | 0

| accepted

Answered
regexp to return folders
Why not just specify the name format to only return names that start with 'sub-', e.g.: S = dir(fullfile(parent_path,'sub-*'));...

6 years ago | 1

| accepted

Answered
How to load files in numerical order?
One simple way is to download my FEX submission natsortfiles: https://www.mathworks.com/matlabcentral/fileexchange/47434-natura...

6 years ago | 1

| accepted

Answered
Use string or character arrays in code?
"Which is preferred and why?" In my opinion character arrays and string arrays have quite different applications, even though p...

6 years ago | 1

| accepted

Answered
Making two data appear side-to-side
Try this: fprintf('%2.4f\t%2.4f\n',[array(:),Sw_C(:)].')

6 years ago | 1

| accepted

Answered
Values not getting printed in csv file
"Values not getting printed in csv file" Actually they are getting printed, your code just keep overwriting them. The problem i...

6 years ago | 0

| accepted

Answered
How delete duplicate strings in cell array
You could easily hide the loop inside cellfun, e.g.: >> fun = @(c)sprintf(' %u',unique(sscanf(sprintf(' %s',c{:}),'%f'))); >> ...

6 years ago | 0

| accepted

Answered
Sum Of Series variables from Vector.
Why not just use a loop?: Q = [1,2;3,4]; R = 5; z = [1;1;3;0;0;5;7;2;0]; % X = [z(1:3:end),z(2:3:end)].'; U = z(3:3:end)....

6 years ago | 0

| accepted

Answered
How to read line with multiple separate headers and tabs in between values
Importing the matrix data as numeric is much easier: opt = {'CollectOutput',true}; [fid,msg] = fopen('sample.txt','rt'); asse...

6 years ago | 0

Answered
saving variables programmatically using list of variable names
The simple solution is to use a comma-separated list: varNames = {'a','b','c'}; save('myMatFile',varNames{:}) % ...

6 years ago | 1

| accepted

Answered
Separating and sorting a character string of magnetometer data (X,Y,Z,temperature) into separate arrays of doubles
>> str = 'X-123.3 Y30.3 Z500.4 t24.5'; >> tkn = regexpi(str,'([A-Z]+)(\S+)','tokens'); >> tkn = vertcat(tkn{:}) tkn = '...

6 years ago | 0

| accepted

Answered
Longest Run Tosses Matlab
Simpler and more efficient: >> N = 17; % number of tosses >> V = randi(2,1,N) % 1=tails 2=heads V = 2 1 2 2 2 2...

6 years ago | 0

Answered
why interp2 report "input grid is not a valid meshgrid" since input grid is generated by meshgrid?
"If anyone knows this issue..." Explanation: you swapped around the order of the variables: [Z,Y] = meshgrid(za,ya); % order: ...

6 years ago | 3

Answered
Error using sscanf Invalid format.
The sscanf documentation explains its syntax as "A = sscanf(str,formatSpec) reads data from str, converts it according to the fo...

6 years ago | 0

| accepted

Answered
Error using fread Invalid file identifier. Use fopen to generate a valid file identifier.
The problem is that your code only uses the filename, but you need to also provide the filepath (otherwise how does MATLAB know ...

6 years ago | 0

| accepted

Answered
MATLAB Colormap doesn't work past 12 colors
" when there are 14 colors in the map, even if the extra two are unused..." That depends on what you are doing. Depending on t...

6 years ago | 1

Answered
Matching unequal cell arrays
Using tables: >> TA = cell2table(A(:)) TA = Var1 ____ 124 252 252 1252 225 225 ...

6 years ago | 0

Answered
Assign a number of random elements to different variables?
Using one cell array is much better than having lots of separate variables, so that is what this answer does. >> v = randperm(4...

6 years ago | 1

Answered
Nested tables with duplicate sub-column names?
The link you gave is unrelated to nested tables: the goal there was to (in some way) merge multiple tables into one table. It i...

6 years ago | 2

| accepted

Answered
Average of multiple cell arrays
Assuming that each cell contains a 1x22051 vector (i.e. they are all the same size): avg_ydft = mean(cat(3,ydft{:}),3) https:/...

6 years ago | 0

| accepted

Answered
How to sum the second position data in an array (50000x2) with the same value in the first position
Method one: accumarray: >> [U,~,G] = uniquetol(A(:,1)); >> S = accumarray(G(:),A(:,2)); >> M = [U,S] M = 2.79314 0.141...

6 years ago | 0

| accepted

Answered
read csv. convert char-array to matrix
Because MATLAB does not handle UCS-2 file I first converted your file to UTF-8 (attached). This imports all of the main matrix ...

6 years ago | 0

Answered
Help with printing output
Instead of adding spaces manually you can set the fieldwidth: hours = [13,22,41]; x = [74.52,30.02,22.68]; y = [77.40,36.30,2...

6 years ago | 0

| accepted

Answered
Access all k-th elements of a n-dimensional array, where the k indexes are defined in a (n-1)-dimensional array.
>> S = size(A); >> [R,C,~] = ndgrid(1:S(1),1:S(2),1); >> X = sub2ind(S,R,C,idx); >> B = A(X) B = 1 11 3 13 ...

6 years ago | 0

| accepted

Load more