Hello everyone. I am working on weather data which i am reading from txt file through matlab code, after that i am comparing log data with that weather data in which i am facing error. I have attached txt file. (Error: Matrix dimensions must agree.)

3 views (last 30 days)
% Calculate mean values from the measurement sequences % Find measurement times where the time interval to the next measurement time is greater than one minute
startIndexMesssequenz = [1 find(diff(timeStampsNoOutLiersRawValuesRANGE0)>duration(0,01:00,0)).'];
  2 Comments
Bob Thompson
Bob Thompson on 2 Apr 2018
Could you please include more of your code than this? It helps us gather context to understand where your variables come from so we are more likely to find the error.
Muhammad Zubair Qureshi
Muhammad Zubair Qureshi on 2 Apr 2018
I have given my complete code, i apologise that the comments are in German language. it also difficult for me to translate

Sign in to comment.

Answers (2)

Muhammad Zubair Qureshi
Muhammad Zubair Qureshi on 2 Apr 2018
%% Initialisierung
close all; clear all;
%Variablen initialisieren timeStampsDistances = []; MID = []; RANGE0 = []; RANGE1 = []; RANGE2 = []; RANGE3 = []; RawF7Temperature = [];
timeStampsWeather = []; Tag = []; Zeit = []; Luftdruck = []; Luftfeuchte = []; Niederschlag = []; Sonnenintensitaet = []; Temperatur = []; UVIndex = []; Windgeschwindigkeit = [];
%% Messwerte einlesen
%Speicherpfade abfragen: disp('Wo sind die Messerwerte gespeichert?') savePath = horzcat(uigetdir('select the Logdata directory'),'\'); %Dialog ffen um Speicherordner festzulegen savePathWithTyp = horzcat(savePath,'*.LOG'); %Speicherpfad inklusive der des zu ffnenden Datentyps ums alle LOG Dateien in einem Ordner zu ffnen
files=dir(savePathWithTyp); %erzeugt eine Variable files, die Informationen zu allen gefunden .LOG Dateien enthlt.
%Fr jede .LOG Datei wird die Schleife einmal durchlaufen und deren Inhalt %mit vertcat(...) an die entsprechenden Variablen angehngt for forCounter = 1:length(files)
activeFile = files(forCounter);
m = memmapfile(strcat(savePath,activeFile.name), ...
'Format', ...
{'uint8',[1 1],'MESSAGEID';...
'uint8',[1,1],'MASK';...
'uint32',[1,1],'RANGE0';...
'uint32',[1,1],'RANGE1';...
'uint32',[1,1],'RANGE2';...
'uint32',[1,1],'RANGE3';...
'uint16',[1,1],'NRANGES';...
'uint8',[1,1],'RSEQ';...
'uint32',[1,1],'DEBUG';...
'uint8',[1,1],'NODETYPE';...
'uint8',[1,1],'TAGID';...
'uint8',[1,1],'ANCHORID';...
'uint16',[1,1],'F7Temperature';...
'uint16',[1,1],'YEARS';...
'uint8',[1,1],'MONTHS';...
'uint8',[1,1],'DAYS';...
'uint8',[1,1],'HOURS';...
'uint8',[1,1],'MINUTES';...
'uint8',[1,1],'SECONDS';...
'uint16',[1,1],'MILLISECONDS'} );
% Zeitstempel der Distanzmesswerte erstellen
year = arrayfun(@(x)(x.YEARS), m.Data)+2000;
months = arrayfun(@(x)(x.MONTHS), m.Data);
days = arrayfun(@(x)(x.DAYS), m.Data);
hours = arrayfun(@(x)(x.HOURS), m.Data);
minutes = arrayfun(@(x)(x.MINUTES), m.Data);
seconds = arrayfun(@(x)(x.SECONDS), m.Data);
milliseconds = arrayfun(@(x)(x.MILLISECONDS), m.Data);
timeStampsDistances = vertcat(timeStampsDistances,datetime(year,months,days,hours,minutes,seconds,milliseconds, 'Format', 'dd.MMMM, yyyy HH:mm:ss.SSS'));
%Message ID einlesen (0 == Anchor-Anchor, 1 == corrected, 2 == uncorrected)
MID = vertcat(MID,arrayfun(@(x)(x.MESSAGEID), m.Data));
%Ranges einlesen
RANGE0 = vertcat(RANGE0,arrayfun(@(x)(x.RANGE0), m.Data));
RANGE1 = vertcat(RANGE1,arrayfun(@(x)(x.RANGE1), m.Data));
RANGE2 = vertcat(RANGE2,arrayfun(@(x)(x.RANGE2), m.Data));
RANGE3 = vertcat(RANGE3,arrayfun(@(x)(x.RANGE3), m.Data));
%F7 Temperatur einlesen
RawF7Temperature = vertcat(RawF7Temperature,arrayfun(@(x)(x.F7Temperature), m.Data));
end
%Clear temporary variables clearvars m year months days hours minutes seconds milliseconds ;
%% Wetterdaten einlesen % Teile des Quellcodes sind dem auto generierten read_weather_values.m % entnommen. Dort sind auch weiterfhrende Komentare zu dem verwendeten % Code zu finden
%Speicherpfade abfragen: disp('Wo sind die Wetterdaten gespeichert?') savePath = horzcat(uigetdir('select the weather data directory'),'\'); %Dialog ffen um Speicherordner festzulegen savePathWithTyp = horzcat(savePath,'*.txt'); %Speicherpfad inklusive der des zu ffnenden Datentyps ums alle LOG Dateien in einem Ordner zu ffnen
files=dir(savePathWithTyp); %erzeugt eine Variable files, die Informationen zu allen gefunden .txt Dateien enthlt.
%Fr jede .LOG Datei wird die Schleife einmal durchlaufen und deren Inhalt %mit vertcat(...) an die entsprechenden Variablen angehngt for forCounter = 1:length(files)
activeFile = files(forCounter);
formatSpec = '%10s%6s%10f%4s%10f%2s%14f%3s%12f%5s%13f%3s%7f%4s%3f%11f%s%[^\n\r]';
%Open the text file.
fileID = fopen(strcat(savePath,activeFile.name),'r');
%Read columns of data according to the format.
%%%%%Original code
% dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'EmptyValue' ,NaN, 'ReturnOnError', false);
%
% %Remove white space around all cell columns.
% dataArray{1} = strtrim(dataArray{1});
% dataArray{2} = strtrim(dataArray{2});
% dataArray{4} = strtrim(dataArray{4});
% dataArray{6} = strtrim(dataArray{6});
% dataArray{8} = strtrim(dataArray{8});
% dataArray{10} = strtrim(dataArray{10});
% dataArray{12} = strtrim(dataArray{12});
% dataArray{14} = strtrim(dataArray{14});
% dataArray{17} = strtrim(dataArray{17});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'EmptyValue' ,NaN, 'ReturnOnError', false); %% Remove white space around all cell columns. dataArray{1} = strtrim(dataArray{1}); dataArray{2} = strtrim(dataArray{2}); dataArray{4} = strtrim(dataArray{4}); dataArray{6} = strtrim(dataArray{6}); dataArray{8} = strtrim(dataArray{8});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Close the text file.
fclose(fileID);
% %Allocate imported array to column variable names % Tag = vertcat(Tag, dataArray{:, 1}); % Zeit = vertcat(Zeit,dataArray{:, 2}); % Luftdruck = vertcat(Luftdruck, dataArray{:, 3}); % Luftfeuchte = vertcat(Luftfeuchte, dataArray{:, 5}); % Niederschlag = vertcat(Niederschlag, dataArray{:, 7}); % Sonnenintensitaet = vertcat(Sonnenintensitaet, dataArray{:, 9}); % Temperatur = vertcat(Temperatur, dataArray{:, 11}); % UVIndex = vertcat(UVIndex, dataArray{:, 13}); % Windgeschwindigkeit = vertcat(Windgeschwindigkeit, dataArray{:, 16});
% Zeitstempel der Wetterwerte erstellen
timeStampsWeather = vertcat(timeStampsWeather, datetime(strcat(Tag,Zeit), 'InputFormat', 'dd.MM.yyyyHH:mm'));
end
%Clear temporary variables clearvars activeFile formatSpec fileID dataArray ans savePathWithTyp savePath Tag Zeit forCounter files;
%% Nachrichten einer Message ID extrahieren %Indexerstellung: Es wird ein Vektor mit 1 und 0 erstellt, der immer eine 1 %bekommt, wenn der gesuchte MessageIDentifier gefunden wird idxAnchorAnchor = (MID == 0); idxCorrected = (MID == 1); idxRaw = (MID == 2);
%Messwerte zusammen fassen %allCorrectedValuesRANGE0 = double(RANGE0(idxCorrected)); allRawValuesRANGE0 = double(RANGE0(idxRaw));
%Zeitstempel vereinzeln: Nicht dreimal hintereinander den gleichen Stempel %speichern, sondern nur noch einmal timeStampsDistances = timeStampsDistances(idxCorrected);
%Clear temporary variables clearvars idxRaw MIDkor idxCorrected idxAnchorAnchor
%% Ausreiser entfernen
%Ergebnisvektor als Spaltenvektor anlegen (:) erzwingt dies y = allRawValuesRANGE0(:);
%Koeffizientenmatrix anlegen X = [x-Werte von 1 bis Anzahl der Distanzmesswerte | Eines Vektor mit der Lnge der Anzahl der Distanzmesswerte % dieser ist fr die Matritzenmultiplikation notwendig, damit das n der % linearen Regression erhalten bleibt] X = [(1:numel(y)).' ones(size(y))];
%Polynomkoeffizienten finden (m und n). D.h. y=m*x+n nach m und n umstellen %und diese berechnen. p enthlt nun den Wert fr m und n p = X\y;
%Abweichungen bestimmen: Messwerte - Modellwerte der Linearen Fkt. delta = y - X*p;
%Ausreiser finden idxOutlier = abs(delta) > 1e3;
%Clear temporary variables clearvars delta y X p
%% Datensatz bereinigen % Ausreiser entfernen
%~ bedeutet elementweise Negieren noOutLiersRawValuesRANGE0 = allRawValuesRANGE0(~idxOutlier); %Zeitstempel der entfernten Distanzwerte lschen timeStampsNoOutLiersRawValuesRANGE0 = timeStampsDistances(~idxOutlier);
%% Mittelwerte aus den Messsequenzen berechnen %Messzeitpunkte finden, bei denen der zeitliche Abstand zum nchsten Messzeitpunkt grer als eine Minute ist startIndexMesssequenz = [1 find(diff(timeStampsNoOutLiersRawValuesRANGE0)>duration(0,01:00,0)).'];
%Nullmatrix mit der Lnge des startIndexMesssequenz Vektors erstellen %meanCorrectedValuesRANGE0 = zeros(numel(startIndexMesssequenz)-1,1); meanRawValuesRANGE0 = zeros(numel(startIndexMesssequenz)-1,1);
%For Schleife von 1 bis Anzahl der Messsequenzen, deren zeitliche Abstand %grer als eine Minute ist for iter=1:(numel(startIndexMesssequenz)-1)
%lokalen Index erzeugen, der die Positionen aller Werte einer
%Messsequenz enthlt
localIdx = startIndexMesssequenz(iter):(startIndexMesssequenz(iter+1)-1);
%Berechnung des Mittelwertes aus einer Messsequenz zusammengesetzt aus
%den Messwerten die durch den loaklen Index adressiert werden
%meanCorrectedValuesRANGE0(iter) = mean(allCorrectedValuesRANGE0(localIdx));
meanRawValuesRANGE0(iter) = mean(noOutLiersRawValuesRANGE0(localIdx));
%Das Ende jeder Messsequemz wird als Messzeitpunkt fr den Mittelwert
%festgelegt
timeStampsForAveragedValues(iter) = timeStampsNoOutLiersRawValuesRANGE0(startIndexMesssequenz(iter+1));
end
%Clear temporary variables clearvars noOutLiersRawValuesRANGE0 timeStampsNoOutLiersRawValuesRANGE0 iter localIdx idxOutlier startIndexMesssequenz
%Parameter 1,2: alte x und y Werte Parameter 3: neue Sttzstellen %interp1(xorg, yorg, xneu). Das Ergebnis wird mit hilfe von ' transponiert; LuftdruckNewSampled = interp1(timeStampsWeather, Luftdruck, timeStampsForAveragedValues)'; LuftfeuchteNewSampled = interp1(timeStampsWeather, Luftfeuchte, timeStampsForAveragedValues)'; NiederschlagNewSampled = interp1(timeStampsWeather, Niederschlag, timeStampsForAveragedValues)'; SonnenintensitaetNewSampled = interp1(timeStampsWeather, Sonnenintensitaet, timeStampsForAveragedValues)'; TemperaturNewSampled = interp1(timeStampsWeather, Temperatur, timeStampsForAveragedValues)'; UVIndexNewSampled = interp1(timeStampsWeather, UVIndex, timeStampsForAveragedValues)'; WindgeschwindigkeitNewSampled = interp1(timeStampsWeather, Windgeschwindigkeit, timeStampsForAveragedValues)';
%% correlation matrix
A = [meanRawValuesRANGE0 LuftdruckNewSampled LuftfeuchteNewSampled NiederschlagNewSampled... SonnenintensitaetNewSampled TemperaturNewSampled UVIndexNewSampled WindgeschwindigkeitNewSampled];
korrelationsmatrixUncorrectedValues = corrcoef(A);
%Clear temporary variables clearvars A return;
%% %readWeatherData_f('Wetterdaten_13_02-16_02.txt');
ZeitWetterdaten = repmat(datetime,size(Tag)); for iter=1:numel(Tag) ZeitWetterdaten(iter) = ... datetime([Tag{iter} ' ' Zeit{iter}], 'InputFormat', 'dd.MM.yyyy HH:mm'); end subplot(3,1,2); plot(ZeitWetterdaten,Temperatur)
%english label title(sprintf('temperature'),'FontSize',16); xlabel('measuring time','FontSize',16); ylabel('temperature [C]','FontSize',16);
subplot(3,1,3); plot(ZeitWetterdaten,Luftfeuchte)
%english label title(sprintf('relative humidity'),'FontSize',16); xlabel('measuring time','FontSize',16); ylabel('relative humidity [%]','FontSize',16);

Akira Agata
Akira Agata on 4 Apr 2018
How about the following script?
% Read your file and erase single quotes (')
fileName = 'timestamNoOutLierRawValuesRANG0.txt';
fid = fopen(fileName,'r');
C = textscan(fid,'%s','Delimiter','\n');
fclose(fid);
C = erase(C{1},'''');
% Create datetime vector
time = datetime(C,...
'InputFormat', 'dd.MMMM, yyyy HH:mm:ss.SSS',...
'Locale', 'en_US');
% Find start index where the time interval to the next is longer than 1min
startIndexMesssequenz = find(diff(time) > minutes(1));

Community Treasure Hunt

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

Start Hunting!