How to loop over a customized function?

3 ビュー (過去 30 日間)
Mohamed Nedal
Mohamed Nedal 2019 年 12 月 20 日
コメント済み: Mohamed Nedal 2019 年 12 月 21 日
Hello everyone,
I wrote a function and I would like to use it on many data at once, so I tried to use the function inside a for loop.
But I got this error
In an assignment A(:) = B, the number of elements in A and B must be the same.
Kindly find the function and my code attached with sample files.
I appreciate your help.
Note: put all the files in the same folder and run the file "semimanual_matching.m".
Basically, I need to run this line
T(n) = V2(dinfo(n).name, V(n));
for several text files, For instance, the function "V2" will run for the first text file "dinfo(1).name" and will take the first value of "V(1)" and put the result in the first cell of "T" ---> T(1).
Currently the error is
Subscripted assignment dimension mismatch.
Error in semimanual_matching (line 12)
T(n) = V2(dinfo(n).name, V(n));
  4 件のコメント
per isakson
per isakson 2019 年 12 月 21 日
編集済み: per isakson 2019 年 12 月 21 日
I miss an instruction on how to use your files. And I miss the file matlab.mat, which is loaded in the first line of the second section of semimanual_matching.
What release of Matlab are you running?
Mohamed Nedal
Mohamed Nedal 2019 年 12 月 21 日
sorry, my bad!
I have edited the describtion and attached the ".mat" file.
I'm using R2017b.

サインインしてコメントする。

採用された回答

per isakson
per isakson 2019 年 12 月 21 日
編集済み: per isakson 2019 年 12 月 21 日
I have modified semimanual_matching so that I don't need to put your text files in a new folder and include that folder in the Matlab search path. My first execution of semimanual_matching_modified produced the following output in the command window (plus two figures with diagrams).
Weak Geomagnetic Storm.
Est. arrival time is 91.6667 hours.
Exp. transit time using G2001 model
is eaither 108.73 hours or 97.69 hours.
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in semimanual_matching_modified (line 16)
T(n) = V2(dinfo(n).name, V(n));
Next I set a breakpoint at the offending line and restarted semimanual_matching_modified. The execution halted before executing T(n) = V2(dinfo(n).name, V(n));. I selected V2(dinfo(n).name, V(n)) with the mouse, right clicked and chose Evaluate Selection from the contect menu. The function V2() returned a column vector. The left hand side, T(n), is a scalar, which explains the error. A vector cannot be assigned to a scalar.
I added the line T = nan(144,3); before the for-loop, replaced T(n) by T(:,n) and clicked Run Section with the cursor at the new line. Output in the command window:
Error using line
Vectors must be the same length.
Error in V2 (line 105)
line([t(row2) t(row2)], ylim, 'LineStyle','--', 'Color', 'g');
Error in semimanual_matching_modified (line 16)
T(:,n) = V2(dinfo(n).name, V(n));
That's a different error. It occured during the third iteration of the loop. n was 3. Part of the output was lost. I guess there is a clc somewhere in the code. Line 104 and 105 of V2 reads
[row2, ~] = find (na_np > 0.08);
line([t(row2) t(row2)], ylim, 'LineStyle','--', 'Color', 'g');
I set a breakpoint at line 105 (dbstop if error didn't work???) and clicked Run Section. The execution halted and I clicked Continue twice. The third time I inspected the values involved
K>> whos row2 na_np t
Name Size Bytes Class Attributes
na_np 144x1 1152 double
row2 0x1 0 double
t 144x1 1152 double
K>> max(na_np)
ans =
0.041
K>>
The error is obviously caused by row2 being empty, which is because max(na_np) is less than 0.08. That in turn has probably something to do with the data in the third text file.
>> dinfo(3).name
ans =
'16122009.txt'
Where semimanual_matching_modified.m now reads
close all; clear; clc
%% read all txt files' names in this folder
dinfo = dir('*.txt');
cac = regexp( {dinfo.name},'^\d+(?=\.txt)', 'start' );
dinfo( cellfun( @isempty, cac ) ) = [];
% filename = zeros(length(dinfo),1);
% for k = 1:length(dinfo)
% filename = dinfo(k).name;
% end
% clear k;
%%
load('matlab.mat');
T = nan(144,3);
for n = 1:3
T(:,n) = V2(dinfo(n).name, V(n));
end
And study
/R2018b
  1 件のコメント
Mohamed Nedal
Mohamed Nedal 2019 年 12 月 21 日
I modified the function and the code and they work!
Thank you so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by