How to call function on each element in structure

2 ビュー (過去 30 日間)
Farah Yasmina
Farah Yasmina 2014 年 7 月 2 日
コメント済み: Farah Yasmina 2014 年 7 月 3 日
Hi,
I am using the function rdir to list the files names ' forces.dat ' in the current directory. It generates a structure D similar to the one returned by the built-in dir command.
The goal is then to go over every name field of the structure D (that includes the relative path as well as the name to the file that was found) and call the function importForces to go over each element in that field (you will find attached the corresponding function importForces.m and script forcesData.m). Now I am having trouble assigning this function to each element in a recursive way, the typical errors I get are :
Error using fopen
First input must be a file name of type char, or a file identifier of type double.
Error in importForces (line 26)
fileID = fopen(filename,'r');
or
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in importForces (line 29)
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'EmptyValue'
,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
I tried many things : converting to table, to cell array, etc, but I still can't wrap my head around how to use that function to go over ever element considering the STRING of characters only.
Any help would be much appreciated!
Thanks, - Farah

採用された回答

dpb
dpb 2014 年 7 月 2 日
編集済み: dpb 2014 年 7 月 2 日
D = rdir('./*/forces.dat');
% Here comes the trouble
for i = 1:length(B)
[t{i},pfx{i},...,torque{i}] = importForces('???');
end
First, you returned the structure in D, then do the loop over length(B) which doesn't exist...looks like should just be something like
for i = 1:length(D)
[t{i},pfx{i},...,torque{i}] = importForces(D(i).name);
end
You'll definitely want to preallocate for all those return values before the looop...
ADDENDUM
BTW, in your importForces function,
function [a1, a2] = importForces(filename, startRow, endRow)
%%Initialize variables.
delimiter = ' ';
if nargin<=2
you've explicitly required all three inputs and it will error on "Not enough arguments" if call it with fewer than 3. If you want to be able to use defaults for the two xxxRow arguments, you must use varargin and write the function definition as
function [a1, a2] = importForces(filename, varargin)
See the doc on varargin and related information on writing functions to use variable number of arguments for the details and examples.
  1 件のコメント
Farah Yasmina
Farah Yasmina 2014 年 7 月 3 日
The loop over length(B) was one of the things I was testing and then forgot to change it back to length(D), sorry about that. Your solution worked, all that was needed to do was to use varargin in my importForces function, and the loop
for i = 1:length(D)
[t{i},pfx{i},...,torque{i}] = importForces(D(i).name);
end
worked perfectly well.
Thank you for that.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by