Multithreading with runspaces: Method calls cause unexpected behavior - powershell

Following the articles Concurrency in PowerShell: Multi-threading with Runspaces and Multithreading PowerShell Scripts, I've been experimenting with multithreading using runspaces, and noticed some behavior that I don't understand.
In the script below, the script block $compute_block simulates an expensive computation by sleeping for a second before returning. Ten threads with this computation are spawned, after which the script waits for all of them to complete and prints the results.
The script can be run with either -Case 1 or -Case 2. In case 1, AddScript is called directly with $compute_block. In case 2, an object is created with a method equivalent to $compute_block, and AddScript is given a script block that calls this method.
threading.ps1
Param([Int] $Case)
Set-StrictMode -Version 2
$start_date = Get-Date
Function Get-Timestamp() {
Return ("{0:N3}" -f ((Get-Date) - $start_date).TotalSeconds)
}
Function Write-Timed([String] $str) {
Write-Host "[$(Get-Timestamp)] $str"
}
$count = 10
$runspace_pool = [RunspaceFactory]::CreateRunspacePool(1, $count)
$runspace_pool.Open()
$compute_block = {
Param($x)
Start-Sleep 1
Return $x
}
# Spawn jobs
$jobs = #()
ForEach($i In 0..($count-1)) {
Write-Timed "Creating job #$i"
$ps = [PowerShell]::Create()
Switch($Case) {
1 {
$job = $ps.AddScript($compute_block).AddArgument($i)
}
2 {
$object = New-Object PSObject `
| Add-Member ScriptMethod Compute $compute_block -PassThru
$job = $ps.AddScript({
Param($o, $x)
Return $o.Compute($x)
}).AddArgument($object).AddArgument($i)
}
}
$job.RunspacePool = $runspace_pool
Write-Timed "Starting job #$i"
$result = $job.BeginInvoke()
Write-Timed "Creating record for job #$i"
$record = New-Object PSObject -Property #{
"Job" = $job;
"Result" = $result;
}
Write-Timed "Adding record for job #$i to list"
$jobs += $record
}
# Wait for all jobs to complete
While($true) {
$running_count = #($jobs | Where-Object { -not $_.Result.IsCompleted }).Count
Write-Timed "Waiting for $running_count/$count jobs"
If(0 -eq $running_count) {
Break
}
Start-Sleep 1
}
# Print results
$i = 0
ForEach($job In $jobs) {
$result = $job.Job.EndInvoke($job.Result)
$job.Job.Dispose()
Write-Timed "Job #$i result: $result"
$i++
}
$runspace_pool.Close()
The results are very different (note the timestamps):
Case 1
PS C:\Users\Miranda\Documents> .\threading.ps1 -Case 1
[0.050] Creating job #0
[0.051] Starting job #0
[0.052] Creating record for job #0
[0.052] Adding record for job #0 to list
[0.053] Creating job #1
[0.053] Starting job #1
[0.056] Creating record for job #1
[0.057] Adding record for job #1 to list
[0.057] Creating job #2
[0.058] Starting job #2
[0.061] Creating record for job #2
[0.062] Adding record for job #2 to list
[0.062] Creating job #3
[0.063] Starting job #3
[0.066] Creating record for job #3
[0.066] Adding record for job #3 to list
[0.067] Creating job #4
[0.067] Starting job #4
[0.070] Creating record for job #4
[0.071] Adding record for job #4 to list
[0.071] Creating job #5
[0.072] Starting job #5
[0.075] Creating record for job #5
[0.076] Adding record for job #5 to list
[0.076] Creating job #6
[0.077] Starting job #6
[0.080] Creating record for job #6
[0.080] Adding record for job #6 to list
[0.081] Creating job #7
[0.081] Starting job #7
[0.084] Creating record for job #7
[0.085] Adding record for job #7 to list
[0.085] Creating job #8
[0.086] Starting job #8
[0.102] Creating record for job #8
[0.103] Adding record for job #8 to list
[0.104] Creating job #9
[0.104] Starting job #9
[0.114] Creating record for job #9
[0.115] Adding record for job #9 to list
[0.119] Waiting for 10/10 jobs
[1.120] Waiting for 0/10 jobs
[1.121] Job #0 result: 0
[1.122] Job #1 result: 1
[1.122] Job #2 result: 2
[1.123] Job #3 result: 3
[1.124] Job #4 result: 4
[1.124] Job #5 result: 5
[1.124] Job #6 result: 6
[1.125] Job #7 result: 7
[1.125] Job #8 result: 8
[1.126] Job #9 result: 9
Case 2
PS C:\Users\Miranda\Documents> .\threading.ps1 -Case 2
[0.080] Creating job #0
[0.117] Starting job #0
[0.120] Creating record for job #0
[0.121] Adding record for job #0 to list
[1.126] Creating job #1
[1.128] Starting job #1
[1.129] Creating record for job #1
[2.130] Adding record for job #1 to list
[2.130] Creating job #2
[2.132] Starting job #2
[2.132] Creating record for job #2
[3.134] Adding record for job #2 to list
[3.135] Creating job #3
[3.136] Starting job #3
[3.137] Creating record for job #3
[4.137] Adding record for job #3 to list
[4.138] Creating job #4
[4.139] Starting job #4
[4.140] Creating record for job #4
[5.141] Adding record for job #4 to list
[5.142] Creating job #5
[5.143] Starting job #5
[5.144] Creating record for job #5
[6.144] Adding record for job #5 to list
[6.145] Creating job #6
[6.146] Starting job #6
[6.147] Creating record for job #6
[7.148] Adding record for job #6 to list
[7.149] Creating job #7
[7.150] Starting job #7
[7.151] Creating record for job #7
[8.152] Adding record for job #7 to list
[8.153] Creating job #8
[8.166] Starting job #8
[8.167] Creating record for job #8
[9.168] Adding record for job #8 to list
[9.169] Creating job #9
[9.170] Starting job #9
[9.171] Creating record for job #9
[10.172] Adding record for job #9 to list
[10.192] Waiting for 0/10 jobs
[10.206] Job #0 result: 0
[10.208] Job #1 result: 1
[10.209] Job #2 result: 2
[10.209] Job #3 result: 3
[10.209] Job #4 result: 4
[10.210] Job #5 result: 5
[10.211] Job #6 result: 6
[10.211] Job #7 result: 7
[10.212] Job #8 result: 8
[10.212] Job #9 result: 9
Case 1 behaves as I would expect — the threads are spawned instantaneously, and the script spends a second in the wait loop before finishing.
However, in case 2, all concurrency seems to be lost. Each iteration of the spawning loop blocks until the spawned thread finishes, and once the wait loop is reached, there's nothing left to wait for. Why is this happening?
Edit:
For the record, I'm working with PS 3.0. As noted by Roman Kuzmin, running case 2 in PS 2.0 produces some very strange errors:
PS>.\threading.ps1 -Case 2
[0.060] Creating job #0
[0.060] Starting job #0
The '=' operator failed: Index was outside the bounds of the array..
At C:\Users\Miranda\Documents\threading.ps1:50 char:14
+ $result = <<<< $job.BeginInvoke()
+ CategoryInfo : InvalidOperation: (System.Manageme...hellAsyncResult:PowerShellAsyncResult) [], RuntimeE
xception
+ FullyQualifiedErrorId : OperatorFailed
C:\Users\Miranda\Documents\threading.ps1 : Index was outside the bounds of the array.
At line:1 char:16
+ .\threading.ps1 <<<< -Case 2
+ CategoryInfo : NotSpecified: (:) [threading.ps1], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException,threading.ps1

I cannot tell exactly why PowerShell works so but I can tell what causes the
problem and how to work around it. In the case 2 the script block
$compute_block is used as the script method in 10 objects/runspaces - this is
the culprit. If we make and use cloned script blocks, i.e.
$compute_block2 = [scriptblock]::Create($compute_block)
$object = New-Object PSObject `
| Add-Member ScriptMethod Compute $compute_block2 -PassThru
then the problem is resolved.
Interestingly, in PowerShell v2 the original script (case 2) does not work at all, it fails with some weird error messages. With a fix it works in v2, as well as in v3. It looks like you hit a problem case to be avoided. I do not remember such a thing documented. It may be a bug or a feature.

Related

Flutter Console Log can't stop running

I am learning flutter recently for my project, and during my experiments I was trying to use other's code from github (link: click here) as my reference. And actually, when I was trying to run the code I downloaded, I didn't know what I encountered, but now when I run another (unconnected, different project) code, even the flutter starting code which has absolutely no error, the program can't stop running once I run the program, and it always gives unending console logs like this:
Launching lib\main.dart on AOSP on IA Emulator in debug mode...
E/flutter ( 7946): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: 'package:flutter/src/widgets/page_view.dart': Failed assertion: line 179 pos 7: 'positions.isNotEmpty': PageController.page cannot be accessed before a PageView is built with it.
E/flutter ( 7946): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:42:39)
E/flutter ( 7946): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:38:5)
E/flutter ( 7946): #2 PageController.page (package:flutter/src/widgets/page_view.dart:179:7)
...
E/flutter ( 7946):
E/flutter ( 7946): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: 'package:flutter/src/widgets/page_view.dart': Failed assertion: line 179 pos 7: 'positions.isNotEmpty': PageController.page cannot be accessed before a PageView is built with it.
E/flutter ( 7946): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:42:39)
E/flutter ( 7946): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:38:5)
E/flutter ( 7946): #2 PageController.page (package:flutter/src/widgets/page_view.dart:179:7)
E/flutter ( 7946): #3 CarouselState.initState. (package:carousel_pro/src/carousel_pro.dart:110:24)
E/flutter ( 7946): #4 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter ( 7946): #5 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 7946): #6 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 7946): #7 _CustomZone.bindUnaryCallbackGuarded. (dart:async/zone.dart:970:26)
E/flutter ( 7946): #8 _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter ( 7946): #9 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
...
instead of just indicating hot restart, and I don't think the logs are important enough. Can I ask for explanation and how to stop this issue? Any help would be appreciated. Thanks a lot guys!

Magento2 file_put_contents

I've just installed (uploaded) a new extension onto my Magento2 store, however after compiling and deploying I constantly run into the following error:
1 exception(s):
Exception #0 (Exception): Warning: file_put_contents(/var/www/html/pub/static/frontend/smart_media/smart_media1/th_TH/css/themes.css): failed to open stream: No such file or directory in /var/www/html/vendor/oyejorge/less.php/lessc.inc.php on line 177
Exception #0 (Exception): Warning: file_put_contents(/var/www/html/pub/static/frontend/smart_media/smart_media1/th_TH/css/themes.css): failed to open stream: No such file or directory in /var/www/html/vendor/oyejorge/less.php/lessc.inc.php on line 177
#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(2, 'file_put_conten...', '/var/www/html/v...', 177, Array)
#1 /var/www/html/vendor/oyejorge/less.php/lessc.inc.php(177): file_put_contents('/var/www/html/p...', '/* Less Variabl...')
#2 /var/www/html/app/code/Rokanthemes/Themeoption/Observer/RefreshStyleObserver.php(58): lessc->compileFile('/var/www/html/a...', '/var/www/html/p...')
#3 /var/www/html/vendor/magento/framework/Event/Invoker/InvokerDefault.php(72): Rokanthemes\Themeoption\Observer\RefreshStyleObserver->execute(Object(Magento\Framework\Event\Observer))
#4 /var/www/html/vendor/magento/framework/Event/Invoker/InvokerDefault.php(60): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Rokanthemes\Themeoption\Observer\RefreshStyleObserver), Object(Magento\Framework\Event\Observer))
#5 /var/www/html/vendor/magento/framework/Event/Manager.php(66): Magento\Framework\Event\Invoker\InvokerDefault->dispatch(Array, Object(Magento\Framework\Event\Observer))
#6 /var/www/html/generated/code/Magento/Framework/Event/Manager/Proxy.php(95): Magento\Framework\Event\Manager->dispatch('layout_generate...', Array)
#7 /var/www/html/vendor/magento/framework/View/Layout/Builder.php(134): Magento\Framework\Event\Manager\Proxy->dispatch('layout_generate...', Array)
#8 /var/www/html/vendor/magento/framework/View/Page/Builder.php(55): Magento\Framework\View\Layout\Builder->generateLayoutBlocks()
#9 /var/www/html/vendor/magento/framework/View/Layout/Builder.php(65): Magento\Framework\View\Page\Builder->generateLayoutBlocks()
#10 /var/www/html/vendor/magento/framework/View/Layout.php(254): Magento\Framework\View\Layout\Builder->build()
#11 /var/www/html/vendor/magento/framework/View/Layout.php(876): Magento\Framework\View\Layout->build()
#12 /var/www/html/vendor/magento/module-cms/Helper/Page.php(171): Magento\Framework\View\Layout->getBlock('page_content_he...')
#13 /var/www/html/vendor/magento/module-cms/Controller/Index/Index.php(43): Magento\Cms\Helper\Page->prepareResultPage(Object(Magento\Cms\Controller\Index\Index\Interceptor), 'home')
#14 /var/www/html/vendor/magento/framework/App/Action/Action.php(107): Magento\Cms\Controller\Index\Index->execute()
#15 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#16 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Cms\Controller\Index\Index\Interceptor->___callParent('dispatch', Array)
#17 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Cms\Controller\Index\Index\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#18 /var/www/html/generated/code/Magento/Cms/Controller/Index/Index/Interceptor.php(26): Magento\Cms\Controller\Index\Index\Interceptor->___callPlugins('dispatch', Array, Array)
#19 /var/www/html/vendor/magento/framework/App/FrontController.php(55): Magento\Cms\Controller\Index\Index\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#20 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#21 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#22 /var/www/html/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#23 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#24 /var/www/html/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(73): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#25 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(135): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#26 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#27 /var/www/html/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#28 /var/www/html/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#29 /var/www/html/vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http->launch()
#30 /var/www/html/pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#31 {main}
I've searched for many solutions, tried to fix permissions, re-deploy static content, remove generated folder, change permissions etc. but to no avail. Some expert insight would be greatly appreciated. Thank you very much
It sounds like potentially the directory target it is trying to write to (smart_media/smart_media1/th_TH/css) does not exist. You may need to modify the extension so that it creates the directory prior to attempting to write to it.
Looks like there are permission issues. You can check magento docs to set the right permissions. You can try these commands
sudo find . -type d -exec chmod 755 {} \;
sudo find . -type f -exec chmod 644 {} \;

PowerShell Workflow Thread Limit

I have a PowerShell workflow that looks something like this, except what each Foo function does is normally very different:
function Foo1
{
"Foo1 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo2
{
"Foo2 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo3
{
"Foo3 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo4
{
"Foo4 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo5
{
"Foo5 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo6
{
"Foo6 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
workflow Invoke-Workflow
{
parallel
{
Foo1
Foo2
Foo3
Foo4
Foo5
Foo6
}
}
Invoke-Workflow
The output of this is:
Foo1 : 10:28:43
Foo2 : 10:28:43
Foo3 : 10:28:44
Foo5 : 10:28:44
Foo4 : 10:28:44
Foo6 : 10:28:46
Showing that the first 5 run immediately, and then the 6th item has to wait for one of the previous items to finish.
I see a lot of documentation that shows how to increase the number of parallel executions in a foreach loop. However how do I increase the number of items that will run in a parallel block?
There doesn't appear to be a way to increase the number of threads that will be used in a parallel block, or if there is, it's difficult to find. A workaround would be to use the Start-Job command. You can use
Start-Job -Scriptblock {#do stuff here}
Or, if it is more code than you want to have inline, you can have it run a script:
Start-Job -Filepath "c:\temp\job.ps1" -ArgumentList 1
It takes some time for each job to spin up, so they don't all start simultaneously. I ran a loop to run Start-Job commands and log the time they started. Each job had a 30 second sleep so that it wouldn't just die immediately (and try to force the number of jobs running to hit the limit of threads). Results varied somewhat, but testing up to 20 showed that it is able to run at least that many simultaneously. In the results below, some were started 28 seconds after the first ones, but remember that the first ones were still running due to the 30 second sleep. So they didn't all start rapid fire, but there were 20 running at once.
#1 started at 5/29/2015 2:17:25 PM
#2 started at 5/29/2015 2:17:25 PM
#3 started at 5/29/2015 2:17:26 PM
#4 started at 5/29/2015 2:17:26 PM
#5 started at 5/29/2015 2:17:27 PM
#7 started at 5/29/2015 2:17:28 PM
#6 started at 5/29/2015 2:17:28 PM
#8 started at 5/29/2015 2:17:31 PM
#9 started at 5/29/2015 2:17:37 PM
#10 started at 5/29/2015 2:17:47 PM
#12 started at 5/29/2015 2:17:53 PM
#13 started at 5/29/2015 2:17:53 PM
#15 started at 5/29/2015 2:17:53 PM
#18 started at 5/29/2015 2:17:53 PM
#11 started at 5/29/2015 2:17:53 PM
#20 started at 5/29/2015 2:17:53 PM
#14 started at 5/29/2015 2:17:53 PM
#16 started at 5/29/2015 2:17:53 PM
#17 started at 5/29/2015 2:17:53 PM
#19 started at 5/29/2015 2:17:53 PM
I did run across a couple of other methods for multi-threading with PowerShell, but this was the easiest to understand and demonstrate. It is probably not the best.

gpus_ReturnGuiltyForHardwareRestart crash

Application crashes in presentFrameBuffer (while running in foreground, no interruption occurring).
It's not crashing in the first frame, it draws for a while then it suddenly crashes.
I don't have exact steps to reproduce, but seems related to drawing something specific, still I have no openGL error reported trough the application, including one error check right before the presentFrameBuffer. If I add glFinish before the presentFrameBuffer will crash in the glFinish.
Application is crashing with EXC_BAD_ACCESS (code=1, address=0x1) and the above callstack without any other error/log/debug info.
Here is the callstack reported at the crash:
Thread 1, Queue : com.apple.main-thread
> #0 0x36871e46 in gpus_ReturnGuiltyForHardwareRestart ()
> #1 0x36872764 in gpusSubmitDataBuffers ()
> #2 0x31eae624 in SubmitPacketsIfAny ()
> #3 0x378a337a in gliPresentViewES ()
> #4 0x325b6df2 in -[EAGLContext presentRenderbuffer:] ()
> #5 0x0052986e in EAGLContext_presentRenderbuffer(EAGLContext*, objc_selector*, unsigned int) ()
> #6 0x000e2a48 in -[EAGLView presentFramebuffer] at /svn/src_svn/GG/iphone/Classes/EAGLView.mm:228
> #7 0x000e4066 in -[GGViewController drawFrame] at /svn/src_svn/GG/iphone/Classes/GGViewController.mm:504
> #8 0x3809ab0a in __NSFireTimer ()
> #9 0x39d36856 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
> #10 0x39d36502 in __CFRunLoopDoTimer ()
> #11 0x39d35176 in __CFRunLoopRun ()
> #12 0x39ca823c in CFRunLoopRunSpecific ()
> #13 0x39ca80c8 in CFRunLoopRunInMode ()
> #14 0x39b9333a in GSEventRunModal ()
> #15 0x3551b288 in UIApplicationMain ()
> #16 0x000e1bae in main at /svn/src_svn/GG/iphone/main.m:14
Anyone has any clue about this one ?
If you are using VAO, this can be caused by the index buffer (element array buffer) referencing vertices beyond the vertex buffer limits (VBO).
Keep in mind that the element array buffer is stored in the VAO, so as long as the VAO is bound, each call to glBindBuffer( GL_ELEMENT_ARRAY_BUFFER ) replaces the index buffer. If you forget to unbind the VAO when you move to your scene's next object you will be altering the VAO of the previous call.
More info over here: http://www.opengl.org/wiki/Vertex_Specification#Index_buffers
And a debugging tip: oversize your vertex buffers, it might turn this crash into a glitch that you can then inspect with the OpenGL ES frame capture tool of XCode (this requires XCode 4.5 and iOS 6).
Looks like the problem was caused by having glEnableClientState(GL_TEXTURE_COORD_ARRAY) for GL_TEXTURE1 but not providing the actual data in the vertex buffer.

import specific columns and range of rows from .dat file

How would I import the data from the fourth row from the following .dat file:
#0 Date-time: 07/06/2011 09:13:53
#1 Recorder: 10T2607
#2 File type: 1
#3 Columns: 3
#4 Channels: 1
#5 Field separation: 0
#6 Decimal point: 0
#7 Date def.: 0 0
#8 Time def.: 0
#9 Channel 1: Temperature(°C) Temp(°C) 3 1
#11 Reconvertion: 0
#19 Line color: 1 2 3 4
#30 Trend Type Number: 1
#33 Limit Temp. Corr. OTCR: 0
1 07.04.11 08:00:00 17,433
2 07.04.11 08:05:00 17,446
3 07.04.11 08:10:00 17,458
4 07.04.11 08:15:00 17,458
So, following the line that begins with #33 I would like to import 17,433 (which should be 17.433) then 17,446 and so on. I have tried to use textscan and headerlines by specifying that the data begins on line 13:
filename = 'Folder\data.dat');
fid = fopen(filename);
data = textscan(fid,'%f\t%f\t%f\t%f\n','Headerlines',13);
fclose(fid);
However, this does not work (in the sense that MATLAB returns an empty array). I guess this is due to the second and third column not being a floating point number, however, it does not work when I specify it to be a string either. What should I try next?
First, note that you have 14 headerlines.
For the data import, you can try the following:
filename = 'Folder\data.dat';
fid = fopen(filename);
data = textscan(fid,'%f\t%s\t%s\t%s','Headerlines',14);
a = cellfun(#(x) str2num(strrep(x, ',', '.')), data{4});
fclose(fid);
This results in
a =
17.4330
17.4460
17.4580
17.4580