Textscan and file paths work on my MacBook, but not on Windows 7

I wrote a script on my MacBook (running MATLAB 2011a) that works just fine (in a loop, it reads a few files of test data, plots relevant parts, then repeats). I need to hand it off to my boss who runs MATLAB 2007 on a PC, but it won't run.
Inside the loop, I've been using textscan to read the data from .csv files, like this:
FID = fopen(['PEMS_event/',files{N,3}]);
event = textscan(FID,'%f %d %s',inf,'delimiter',',');
event_t = event{1};
event_ID = event{2};
fclose(FID);
where files{N,3} refers to a matrix of .csv filenames defined earlier.
The fopen command seems to work (FID is a positive integer), but 'event' is an array of empty arrays/cells.
I suspect the problem has to do with the file path, but the same problem occurred when I moved the files directly into the current directory and removed the folder 'PEMS_event/' from the path specification.
I also tried using xlsread (I understand it works well with windows, not with macs) with the file in the current directory like this:
[data text] = xlsread('bb_event_2013-08-19_15-16-09.csv');
but this just gave me an error:
??? XLSREAD unable to open file bb_event_2013-08-19_15-16-09.csv.
Undefined function or variable 'name'.
Help? I'm confused. Thanks in advance.

 Accepted Answer

Cedric
Cedric on 22 Aug 2013
Edited: Cedric on 22 Aug 2013
For building the full path, use FULLFILE. In FOPEN, specify the permission explicitly, so you don't have to count on compatibility of default args between versions. Could you copy/paste a chunk of this file that you are trying to read, so we can check the formatSpec? For CSV files, use CSVREAD or DLMREAD instead of XLSREAD.

3 Comments

Sure, here's what the files look like:
12.290, 26, Data start
1641.048, 28, Sub-test started
2092.079, 29, Sub-test stopped
2947.900, 28, Sub-test started
3268.531, 29, Sub-test stopped
4035.932, 28, Sub-test started
4215.202, 9, Fuel reposition
4382.522, 29, Sub-test stopped
5195.953, 28, Sub-test started
5625.284, 29, Sub-test stopped
6297.775, 28, Sub-test started
6824.856, 29, Sub-test stopped
8029.087, 28, Sub-test started
8867.668, 29, Sub-test stopped
Another of the files the script opens later on is of this format:
# Description: H4_Jet_Wand_1_6-18-2013
# type cal (in 5 seconds)
# 'log' command starts logging
log
#logging
##,##
# ,.087,.263,13.6, ,.00381,.1,.1,.1,1,.0106
seconds,CO,CO2,PM,Flow,FlueTemp,TCpot,TC1,TC2,TC3,Voc
1.00,300,3514,436,9230,7108,65536,65536,147,65536,0
3.00,306,3340,434,9235,7103,65536,65536,147,65536,0
5.00,307,3192,434,9215,7112,65536,65536,148,65536,0
I wasn't using csvread because it can't accept non-numeric data, that is it gives an error on encountering header lines or columns of strings. With textscan I could just go through those columns or rows specifying a string format, and then look at the actual data after line 8 by issuing the command again (I'll put that chunk of code below too). If there is a workaround using dlmread or csvread, I see no reason that couldn't fix the script but I haven't gotten those functions to work either.
FIDp = fopen(['PEMs_data/',files{N,2}]);
PEMShead = textscan(FIDp,'%s %s %s %s %s %s %s %s %s %s %s',8,'delimiter',',');
PEMSdata = textscan(FIDp,'%f %f %f %f %f %f %f %f %f %f %f','delimiter',',');
fclose(FIDp);
My friend Julie figured it out! On my Mac, 'inf' tells textscan to go to the end of the file, but on this PC it's -1. Changing that, the script runs fine.
FID = fopen(['PEMS_event/',files{N,3}]);
event = textscan(FID,'%f %d %s',-1,'delimiter',',');
event_t = event{1};
event_ID = event{2};
fclose(FID);
Thanks for your time!
Great that she found out, because I had no memory that Inf was not supported in the 2007 version!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!