Labview and Matlab for PT

Just a place to discuss with Matlab and Labview

ad

在GUIDE擺置radio button的方法

  

  這個影片介紹了如何在GUIDE中,建立互相獨立的radio button,說真的,我是今天才找到這個資料,之前也是百思不得其解,好在youtube有人分享。

  不過,因為這個不是英文也不是中文,所以應該聽不懂他在說啥,但是就算不聽看操作步驟也應該是可以懂得!

讀取有Header的txt檔

今天我有一個檔案長得如下:



frame force angle
180.000000 25.907727 -96.707100
198.000000 52.657283 -97.686900
206.000000 79.306087 -97.134200
216.000000 105.920720 -96.028800
234.000000 132.531830 -95.501200
252.000000 159.259191 -95.023900
271.000000 185.936060 -94.496300
293.000000 212.583572 -94.194800
312.000000 239.251047 -92.586900
332.000000 265.956685 -91.506600
357.000000 292.655864 -91.556900
364.000000 265.886229 -91.255400
369.000000 239.255744 -91.582000
376.000000 212.594141 -92.813000
384.000000 185.939583 -92.586900
394.000000 159.297355 -93.215000
406.000000 132.574103 -93.390900
421.000000 105.981781 -93.943600
434.000000 79.340140 -94.747500
468.000000 52.674016 -96.104100
501.000000 25.987401 -97.360300



  在matlab裡面一定沒有辦法用既有的textread讀取,因為第一行的文字!於是我寫個個跳過第一行的程式!如下:


function mat_a = get_to_mat(filename,l)
fp = fopen(filename);
current_l = 1;
while 1
content = fgetl(fp);
if current_l <= l
tile = content;
current_l = current_l +1;
elseif current_l > l & ischar(content) == 1
mat_a(current_l - l,:) = str2num(content);
current_l = current_l +1;
elseif ischar(content) == 0
break
end
end
fclose(fp);;



  概念很簡單,就是輸入要跳過的行數,然後以行數來計算一行一行存成矩陣!但是這時候我又想到,剛好今天我這個檔案非常的小,行數也很短,如果我今天拿上萬行的數據執行,執行到死都執行不完!
  百思不得其解後,google了許久,在matlab官方網站看到了有人分享的寫法:



function [header, data] = hdrload(file)


% HDRLOAD Load data from an ASCII file containing a text header.
% [header, data] = HDRLOAD('filename.ext') reads a data file
% called 'filename.ext', which contains a text header. There
% is no default extension; any extensions must be explicitly
% supplied.
%
% The first output, HEADER, is the header information,
% returned as a text array.
% The second output, DATA, is the data matrix. This data
% matrix has the same dimensions as the data in the file, one
% row per line of ASCII data in the file. If the data is not
% regularly spaced (i.e., each line of ASCII data does not
% contain the same number of points), the data is returned as
% a column vector.
%
% Limitations: No line of the text header can begin with
% a number. Only one header and data set will be read,
% and the header must come before the data.
%
% See also LOAD, SAVE, SPCONVERT, FSCANF, FPRINTF, STR2MAT.
% See also the IOFUN directory.


% check number and type of arguments
if nargin < 1
error('Function requires one input argument');
elseif ~isstr(file)
error('Input must be a string representing a filename');
end


% Open the file. If this returns a -1, we did not open the file
% successfully.
fid = fopen(file);
if fid==-1
error('File not found or permission denied');
end


% Initialize loop variables
% We store the number of lines in the header, and the maximum
% length of any one line in the header. These are used later
% in assigning the 'header' output variable.
no_lines = 0;
max_line = 0;


% We also store the number of columns in the data we read. This
% way we can compute the size of the output based on the number
% of columns and the total number of data points.
ncols = 0;


% Finally, we initialize the data to [].
data = [];


% Start processing.
line = fgetl(fid);
if ~isstr(line)
disp('Warning: file contains no header and no data')
end;
[data, ncols, errmsg, nxtindex] = sscanf(line, '%f');


% One slight problem, pointed out by Peter vanderWal: If the
% first character of the line is 'e', then this will scan as
% 0.00e+00. We can trap this case specifically by using the
% 'next index' output: in the case of a stripped 'e' the next
% index is one, indicating zero characters read. See the help
% entry for 'sscanf' for more information on this output
% parameter. We loop through the file one line at a time until
% we find some data. After that point we stop checking for
% header information. This part of the program takes most of the
% processing time, because fgetl is relatively slow (compared to
% fscanf, which we will use later).
while isempty(data)|(nxtindex==1)
no_lines = no_lines+1;
max_line = max([max_line, length(line)]);
% Create unique variable to hold this line of text information.
% Store the last-read line in this variable.
eval(['line', num2str(no_lines), '=line;']);
line = fgetl(fid);
if ~isstr(line)
disp('Warning: file contains no data')
break
end;
[data, ncols, errmsg, nxtindex] = sscanf(line, '%f');
end % while


% Now that we have read in the first line of data, we can skip
% the processing that stores header information, and just read
% in the rest of the data.
data = [data; fscanf(fid, '%f')];
fclose(fid);


% Create header output from line information. The number of lines
% and the maximum line length are stored explicitly, and each
% line is stored in a unique variable using the 'eval' statement
% within the loop. Note that, if we knew a priori that the
% headers were 10 lines or less, we could use the STR2MAT
% function and save some work. First, initialize the header to an
% array of spaces.
header = setstr(' '*ones(no_lines, max_line));
for i = 1:no_lines
varname = ['line' num2str(i)];
% Note that we only assign this line variable to a subset of
% this row of the header array. We thus ensure that the matrix
% sizes in the assignment are equal. We also consider blank
% header lines using the following IF statement.
if eval(['length(' varname ')~=0'])
eval(['header(i, 1:length(' varname ')) = ' varname ';']);
end
end % for


% Resize output data, based on the number of columns (as returned
% from the sscanf of the first line of data) and the total number
% of data elements. Since the data was read in row-wise, and
% MATLAB stores data in columnwise format, we have to reverse the
% size arguments and then transpose the data. If we read in
% irregularly spaced data, then the division we are about to do
% will not work. Therefore, we will trap the error with an EVAL
% call; if the reshape fails, we will just return the data as is.
eval('data = reshape(data, ncols, length(data)/ncols)'';', '');




  簡單的說,這個程式先一行一行判斷是否為string(if ~isstr(line)),然後在以data = [data; fscanf(fid, '%f')];讀入數值資料,並且會以[data, ncols, errmsg, nxtindex] = sscanf(line, '%f');(其中line是先讀入一行)去看ncols。

  最後再把讀入的數值data資料用reshape的方式(data = reshape(data, ncols, length(data)/ncols);)完成。速率的確加快許多。

與Matlab textread相同功能的fscanfMat(scilab)

  如題,最近在研究FreeMat和Scilab兩個免費又和matlab相似的軟體(應該說是clone)

matlab中的textread對於我讀大量數據的時候非常方便,自動可以存成矩陣,剛剛google了一下,發現scilab中的fscanfMat指令有相同功能。

  不過現在美中不足的是,scilab介面改由java製成,plot指令居然會造成crash,哇哩勒!連plot都不能用那還搞啥東東!?於是只好將回version4使用!(難怪下載的時候發現scilab怎麼變得如此肥!)

失誤?!

今天試圖來做一個檔案處理,預備將我要的檔案形式讀入,我的檔案很簡單,如下:

name 腿長 腳踝長
SYC 47 19
YFT 41 13
ZJY 45 15
LSP 44 14
CCH 51 16
LIU 42 14.5
KMY 40 14
LC 41 13.5
LGP 46 15
CCL 40 13.5
CIC 49 16
ZKY 48 15


  這是在記事本或是notepad顯示的資料,前面是姓名並且腿長和腳踝長中間以'\t'做區隔。
  
  我的目的是要將檔案如matlab中textread指令一樣,將資料讀入並以name Leg Ankle,三個陣列儲存,然後第一行不要。



clear all
fd = fopen('I:\Leglength.txt','r');
m = 1;
n = 1;
while 1
if m == 1
tile = fgets(fd);
m=m+1;
elseif fgets(fd) == -1
break
else
content=fgets(fd);
[name(n) Leg(n) Ankle(n)]=strread(content,'%s %f %f');
n=n+1;
end
end
fclose(fd);



  想法非常簡單m=1單純就用在第一行,然後把第一行資料丟到title裡面去。其他就是[name(n) Leg(n) Ankle(n)]=strread(content,'%s %f %f');依照這樣格式丟入,然後如果讀到最後一行fgets(fd) == -1,則結束此程式。

  執行後結果,以name來看



>> name

name =

'YFT' 'LSP' 'LIU' 'LC' 'CCL' 'ZKY'



  哇哩勒,what happened??為啥麼只讀了3 5 7 9 11 13行,發生了啥事?百思不得其解,試了又試,情況依舊,於是我開始懷疑起elseif fgets(fd) == -1,所以我把程式改寫成



clear all
fd = fopen('I:\Leglength.txt','r');
m = 1;
n = 1;
while 1
content = fgets(fd)
if m == 1
tile = content;
m=m+1
elseif ischar(content) == 1
[name(n) Leg(n) Ankle(n)]=strread(content,'%s %f %f');
n=n+1;
end
if content == -1
break
end
end
fclose(fd);




  果不其然,這次完全沒有問題




>> name

name =

'SYC' 'YFT' 'ZJY' 'LSP' 'CCH' 'LIU' 'KMY' 'LC' 'LGP' 'CCL' 'CIC' 'ZKY'





  那也就是說,把elseif fgets(fd) == -1當作判斷句的時候,程式就會讀了一行,所以在跑之後的[name(n) Leg(n) Ankle(n)]=strread(content,'%s %f %f'),所讀到的就是下一行檔案內的東西!