How can i read data from a text file with parfor or spmd .i have to read as two workers and to combine the result lastly.please reply soon..

How can i read data from a text file with parfor or spmd .i have to read as two workers and to combine the result lastly.please reply soon..

Answers (1)

Something like this?
spmd
fh = fopen(sprintf('file_%d.txt', labindex), 'rt');
data = fscanf(fh, '%f');
fclose(fh);
gcat(data, 1); % GCAT combines the data
end

2 Comments

spmd fh = fopen(sprintf('in_file2_%d.txt', labindex), 'rt'); data = fscanf(fh, '%f'); fclose(fh); gcat(data, 1); % GCAT combines the data end
I have tried this code,But i got the following errors...please tell me the reason...
Error using spmd_feval (line 8) Error detected on lab(s) 1
Error in macode_parallel (line 2) fh = fopen(sprintf('in_file2_%d.txt', labindex), 'rt');
Caused by:
Invalid file identifier. Use fopen to generate a valid file
identifier.
Error stack:
(No remote error stack)
Looks like your file name is incorrect. You could try adding
spmd
fname = sprintf(...);
fh = fopen(fname, 'rt');
if fh == -1
error('Could not read file: %s', fname);
end
...
end
This will indicate which file couldn't be found.

Sign in to comment.

Categories

Asked:

on 20 Jun 2013

Community Treasure Hunt

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

Start Hunting!