how to read svc file in matlab?
3 views (last 30 days)
Show older comments
how to read .svc file in matlab. i have to read the .svc file but it give error in path of file given is the code that i follow for reading svc plz tell me what is the error in it. thanks alot
function Y = read_SVC_file(pth_svc, norm)
% This function loads *.svc data into matrix.
% pth_svc - path to the *.svc file
% norm - if set to 1 the x-coordinate will be normalized to 0 and
% y-coordinate will be subtracted by its mean (default: 0)
% Y - matrix with the SVC data
% Y(:,1) - X coordinate
% Y(:,2) - Y coordinate
% Y(:,3) - time stamp
% Y(:,4) - on/off state
% Y(:,5) - azimuth
% Y(:,6) - altitude
% Y(:,7) - pressure
%%Paths and variables
if((nargin < 2) || isempty(norm))
norm = 0;
end
%%Read the data from *.svc file
if(~exist(pth_svc,'file'))
error(['File ' pth_svc ' does not exist.']);
end
FID = fopen(pth_svc,'r');
contents = textscan(FID,'%n%n%n%d8%n%n%n','HeaderLines',1);
fclose(FID);
%%Get the initial time
min_TS = min(contents{3});
%%Store the data into the matrix
Y = [contents{2}, contents{1}, contents{3}-min_TS, cast(contents{4}, ...
'double'), contents{5}, contents{6}, contents{7}];
%%Normalize X-Y coordinates if necessary
if(norm)
Y(:,1) = Y(:,1)-min(Y(:,1));
Y(:,2) = Y(:,2)-mean(Y(:,2));
end
6 Comments
Answers (2)
Guillaume
on 7 Sep 2017
In essence, the error tells you that matlab failed to open the file. There can be several reasons for that, the most common being that the path given is incorrect.
Despite your statement, it is very likely that your path is not correct. You are using a path that looks very much like a folder instead of a file. Note that exist(xx, 'file') will return true if xx is a file or a folder.
Furthermore, it looks like you don't understand how to use functions. Never replace input arguments of function by hard coded values in the function codes. You're losing all the benefit of functions. So, please restore the function code to what it was originally (as in your first post), and call the function with:
folder = 'M:\pawh\PaHaW\PaHaW_public';
filename = '????.svc'; %replace with filename of the .svc file, including extension
result = read_SVC_file(fullfile(folder, filename))
Other possible reasons for matlab failing to open the file
- You do not have permission to open the file but are allowed to list the directory content. That would be an odd configuration but is possible so check the security settings of the file.
- Another program has opened the file with exclusive access, locking it and preventing matlab from opening it (Excel is very good at that!). So, check that no other program has the file open.
- Hardware failure
But as said, it is extremely likely that your path is not correct.
0 Comments
OCDER
on 7 Sep 2017
Edited: OCDER
on 7 Sep 2017
It looks like you're specifying a file path (pth_svc), but trying to open it as a file in fopen, which would give you an error. The error is not caught after fopen, hence textscan throws the error instead when trying to open an FID = -1. If you want to specify a path, then search for the *.svc files within the path and process those files. I'd use something like this:
SvcFileList = dir([pth_svc filesep '*.svc'])
Here's the new code, but I hope you don't mind that I changed some variable and function names.
My coding style (but feel free to adapt a different style):
- avoid underscores in names if possible, as they're harder to type and view.
- variables names = upper camel case (ex: FileName)
- function name = lower camel case (ex: readSVC)
function Y = readSVC(FileName, Norm)
% This function loads *.svc data into matrix.
% FileName - file name of the svc file
% Norm - if set to 1 the x-coordinate will be normalized to 0 and
% y-coordinate will be subtracted by its mean (default: 0)
% Y - matrix with the SVC data
% Y(:,1) - X coordinate
% Y(:,2) - Y coordinate
% Y(:,3) - time stamp
% Y(:,4) - on/off state
% Y(:,5) - azimuth
% Y(:,6) - altitude
% Y(:,7) - pressure
%%Paths and variables
if nargin < 2 || isempty(Norm)
Norm = 0;
else
if Norm ~= 0 && Norm ~= 1
error('Norm must be 0 or 1.')
end
end
%%Read the data from *.svc file
[FID, MSG] = fopen(FileName, 'r');
if FID < 0
error('%s\n FileName = "%s"', MSG, FileName);
end
Contents = textscan(FID, '%n%n%n%d8%n%n%n', 'HeaderLines', 1);
fclose(FID);
%%Get the initial time
InitTime = min(Contents{3});
%%Store the data into the matrix
Y = [Contents{2}, Contents{1}, Contents{3}-InitTime, ...
cast(Contents{4}, 'double'), Contents{5}, Contents{6}, Contents{7}];
%%Normalize X-Y coordinates if necessary
if Norm
Y(:, 1) = Y(:, 1) - min(Y(:, 1));
Y(:, 2) = Y(:, 2) - mean(Y(:, 2));
end
3 Comments
Walter Roberson
on 21 Sep 2017
When you are calling the routine, do not use the word "function". "function" is only used to create the function.
Rik
on 21 Sep 2017
Also, you can't provide a path with a wildcard (*) and expect Matlab to know what you want. As far as I can see, this code will try to find the file '*.svc', which of course will fail.
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!